Official

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

GPT 5.4 High

Overview

For each string, look only at the first character and count how many strings fall into each of the 26 groups from a to z.
Finally, output the size of the group with the largest count.

Analysis

In this problem, the entire content of each string is not needed. The grouping condition is solely whether the first characters are the same or not.

For example, if the strings are

  • apple
  • ant
  • banana
  • book
  • cat

then,

  • Starting with a: 2 strings
  • Starting with b: 2 strings
  • Starting with c: 1 string

and the answer is \(2\).

Key Observation

  • Groups are determined by the first character
  • Since the first character is a lowercase English letter, there are only 26 possible groups from a to z
  • Even if the same string appears multiple times, as stated in the problem, they are counted separately, so we simply count the occurrences

Naive Approach

You could classify each string by its first character and store them in lists.
However, since all we need in this problem is the “maximum count,” there is no need to actually store the strings.

How to Solve

Prepare 26 counters for a through z, and for each string:

  • Extract the first character
  • Increment the counter corresponding to that character by 1

This is sufficient.
Finally, output the maximum value among the 26 counters to get the answer.

Algorithm

  1. Initialize an array cnt of length 26 with zeros

    • cnt[0] is the number of strings starting with a
    • cnt[1] is the number of strings starting with b
    • cnt[25] is the number of strings starting with z
  2. For each string s, look at the first character s[0]

  3. Convert a to 0, b to 1, …, z to 25 using ord(s[0]) - ord('a'), and increment the corresponding cnt by 1

  4. Finally, output max(cnt)

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(1)\)

The size of cnt is always fixed at 26, so the additional memory used is constant.

Implementation Notes

  • Since each string has length at least 1, s[0] can be used safely
  • input().strip() is used to remove trailing newlines
  • ord() is used to compute the array index from the first character

For example, in the case of 'c':

  • ord('c') = 99
  • ord('a') = 97

So 99 - 97 = 2, and we increment cnt[2].

Source Code

import sys

def main():
    input = sys.stdin.readline
    n = int(input())
    cnt = [0] * 26
    for _ in range(n):
        s = input().strip()
        cnt[ord(s[0]) - ord('a')] += 1
    print(max(cnt))

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.4-high.

posted:
last update: