A - 財布の中身 / Contents of the Wallet 解説 by admin
gpt-6-astra-mediumOverview
We check whether the string \(S\) contains both C and B. If both are present, output Yes; if either is missing, output No.
Analysis
Having at least \(1\) coin is equivalent to \(S\) containing C. Similarly, having at least \(1\) banknote is equivalent to \(S\) containing B.
Therefore, the problem can be solved by checking whether both of the following conditions are satisfied:
- \(S\) contains
C. - \(S\) contains
B.
For example, if \(S =\) CCB, both are contained, so the answer is Yes. If \(S =\) BBB, there are no coins, so the answer is No.
A naive approach of examining the string character by character easily runs within the time limit for this problem. While it can also be solved by counting the number of coins and banknotes, counting them is unnecessary because we only need to know whether they exist.
Note that checking if “at least one of them exists” is incorrect. Since \(S\) is always a non-empty string consisting only of C and B, that condition would always evaluate to Yes.
Algorithm
- Read the string \(S\) from the input.
- Check whether there are coins using
'C' in Sand store the result inhas_c. - Check whether there are banknotes using
'B' in Sand store the result inhas_b. - If both
has_candhas_bare true, outputYes; otherwise, outputNo.
Complexity
- Time Complexity: \(O(N)\)
Each search usingininspects up to \(N\) characters. Since the search is performed \(2\) times, the overall time complexity is \(O(N)\). - Space Complexity: \(O(N)\)
Storing the input string takes \(O(N)\) space. The additional space required for the check is \(O(1)\).
Implementation Details
In Python, using
inallows you to check whether a specified character is contained in a string.Use
and, notor, to check the condition that “both hold true.”When splitting the input by whitespace and newlines using
sys.stdin.read().split(),data[0]corresponds to \(N\) anddata[1]corresponds to \(S\). Since this solution does not directly use \(N\), converting it to an integer is not necessary.Ensure the output matches the case-sensitive specification, printing either
YesorNo.Source Code
import sys
def main():
data = sys.stdin.read().split()
S = data[1]
has_c = 'C' in S
has_b = 'B' in S
print('Yes' if has_c and has_b else 'No')
main()
This editorial was generated by gpt-6-astra-medium.