Official

A - 財布の中身 / Contents of the Wallet Editorial by admin

gpt-6-astra-medium

Overview

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 both C and B are present
  • CCC: No, because B is missing
  • B: No, because C is 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

  1. Read \(N\) and the string \(S\).
  2. Check whether coins and bills are each present using 'C' in s and 'B' in s.
  3. If both conditions are satisfied, output Yes; otherwise, output No.

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)\)
    Each in operation 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 and to verify that both C and B are present.

  • The output is case-sensitive, so print Yes or No exactly as specified.

  • Although \(N\) is read as input, it does not need to be used directly in the check since in searches 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.