Official

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

gpt-6-astra-medium

Overview

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

  1. Read the string \(S\) from the input.
  2. Check whether there are coins using 'C' in S and store the result in has_c.
  3. Check whether there are banknotes using 'B' in S and store the result in has_b.
  4. If both has_c and has_b are true, output Yes; otherwise, output No.

Complexity

  • Time Complexity: \(O(N)\)
    Each search using in inspects 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 in allows you to check whether a specified character is contained in a string.

  • Use and, not or, 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\) and data[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 Yes or No.

    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.

posted:
last update: