Official

A - アルファベット分類 / Alphabet Classification Editorial by admin

Claude 4.6 Opus (Thinking)

Overview

This is a problem where you group \(N\) strings by their first character and find the size of the group containing the most strings.

Analysis

  • For each string, the only thing that matters is the first character. The rest of the string has no effect on the grouping.
  • Since the first character is a lowercase English letter, there are at most \(26\) groups (a through z).
  • Even if the same string appears multiple times, we do not remove duplicates — we count each occurrence as-is.

Concrete example: If the strings are apple, ant, banana, avocado, berry: - Group starting with a: apple, ant, avocado3 strings - Group starting with b: banana, berry2 strings - The maximum is the a group with 3, which is the answer.

Since this problem can be solved fast enough by simply counting the first character of each string, no special techniques are needed.

Algorithm

  1. Read the \(N\) strings one by one.
  2. Extract the first character s[0] of each string and count the number of occurrences of that character.
  3. After processing all strings, output the maximum count.

In Python, you can use collections.Counter to concisely manage the occurrence count for each key.

from collections import Counter

N = int(input())
c = Counter()
for _ in range(N):
    s = input()
    c[s[0]] += 1  # Increment the count for the first character
print(max(c.values()))  # Output the maximum count
  • c[s[0]] += 1 records how many strings exist for each first character.
  • Finally, max(c.values()) retrieves the maximum number of strings among all groups.

Complexity

  • Time complexity: \(O(N)\) — Extracting the first character and updating the count is done in \(O(1)\) per string, and at the end we find the maximum among at most \(26\) values.
  • Space complexity: \(O(1)\) — The keys stored in the counter are at most \(26\) types of lowercase English letters, so the size is constant. (There is no need to store the input strings themselves.)

Implementation Notes

  • Instead of using Counter, you can also solve this by preparing an array (list) of size \(26\) and using ord(s[0]) - ord('a') as the index.

  • Since \(N \geq 1\) is guaranteed, there is no concern about max(c.values()) being called on an empty collection.

    Source Code

from collections import Counter

N = int(input())
c = Counter()
for _ in range(N):
    s = input()
    c[s[0]] += 1
print(max(c.values()))

This editorial was generated by claude4.6opus-thinking.

posted:
last update: