Official

A - 旅の記録 / Travel Records Editorial by admin

gpt-6-astra-medium

Overview

We examine the string \(S\) from the beginning and count the number of uppercase English letters. This is the number of times Takahashi visited foreign countries.

Analysis

From the problem statement, uppercase English letters represent visits to foreign countries, while lowercase English letters represent domestic visits. Therefore, we can solve the problem by checking whether each character is an uppercase letter.

In Python, we can check if a character c is an uppercase letter using the following condition:

'A' <= c <= 'Z'

For example, when \(S = \texttt{aBCdA}\), the uppercase letters are B, C, and A, totaling \(3\), so the answer is \(3\).

For this problem, the naive approach of checking every character will easily run in time. Since \(N \leq 100\), no special optimizations for speed are necessary.

Note that we need to find the number of visits, not the number of distinct foreign countries. Even if the same uppercase letter appears multiple times, each occurrence must be counted.

Algorithm

  1. Read \(N\) and the string \(S\).
  2. For each character c in \(S\), check whether 'A' <= c <= 'Z' holds.
  3. Print the number of characters that satisfy the condition.

Since we inspect each character exactly once and count it only if it is an uppercase letter, we obtain the correct count.

Complexity

  • Time complexity: \(O(N)\)
    We inspect each character of the string once.
  • Space complexity: \(O(N)\)
    Required to store the input string. The additional space needed for the counting process itself is \(O(1)\).

Implementation Notes

In the accepted solution, the number of uppercase letters is computed using the following expression:

sum(1 for c in s if 'A' <= c <= 'Z')

This iterates over each character of s and adds \(1\) if it is an uppercase letter. Because it uses a generator expression, there is no need to construct an intermediate list for counting.

Additionally, since we iterate directly over the string, we do not need to use the input n for the loop.

Source Code

import sys

def main():
    data = sys.stdin.read().split()
    n = int(data[0])
    s = data[1]
    print(sum(1 for c in s if 'A' <= c <= 'Z'))

main()

This editorial was generated by gpt-6-astra-medium.