A - 財布の中身 / Contents of the Wallet Editorial by admin
gpt-6-astra-mediumOverview
Determine whether the string \(S\) contains both C and B. If both are present, output Yes; otherwise, output No.
Analysis
Having coins corresponds to “\(S\) contains C”, and having bills corresponds to “\(S\) contains B”.
All that is required is whether there is at least \(1\) of each, and there is no need to count the exact number. In Python, you can determine whether each character is present using 'C' in s and 'B' in s.
For example:
CCB:Yes, because bothCandBare presentCCC:No, becauseBis missingB:No, becauseCis missing
A naive check scanning the string from the beginning is well within the time limit, and no special optimization is required. However, combining the conditions with or would check if “at least one of them is present”, resulting in a wrong answer. Since both are required in this problem, use and.
Algorithm
- Read \(N\) and the string \(S\).
- Check whether coins and bills are each present using
'C' in sand'B' in s. - If both conditions are satisfied, output
Yes; otherwise, outputNo.
This check directly corresponds to the required condition “there is at least \(1\) coin and at least \(1\) bill”, so it produces the correct answer.
Complexity
- Time Complexity: \(O(N)\)
Eachinoperation checks up to \(N\) characters. Since the check is performed at most \(2\) times, the overall time complexity is \(O(N)\). - Space Complexity: \(O(N)\)
\(O(N)\) memory is used to store the input string \(S\). The additional space excluding input storage is \(O(1)\).
Implementation Details
Use
andto verify that bothCandBare present.The output is case-sensitive, so print
YesorNoexactly as specified.Although \(N\) is read as input, it does not need to be used directly in the check since
insearches the entire string.Source Code
# 問題文から独立に実装
n = int(input())
s = input().strip()
print("Yes" if 'C' in s and 'B' in s else "No")
This editorial was generated by gpt-6-astra-medium.