A - アルファベット分類 / Alphabet Classification Editorial by admin
gpt-5.3-codexOverview
Classify each string by its “first character” and count the number of strings in each group.
The problem asks you to output the size (maximum value) of the group with the most strings.
Analysis
The key insight is that you don’t need to look at the entire string; looking at only the first character is sufficient.
The grouping condition in the problem is solely “whether the first characters are the same,” so for example "apple" and "ant" belong to the same 'a' group.
Since there are only 26 types of lowercase English letters, from 'a' to 'z',
we can manage “the number of times each character appears as a first character” using an array of size 26.
Comparison with a Naive Approach
- An approach like comparing all strings to form groups is unnecessary (too many comparisons, inefficient).
- Removing duplicates using a set is incorrect (WA). This is because the problem states that “even if the same string appears multiple times, each occurrence is counted separately.”
Therefore, the shortest and most reliable approach is to extract only the first character of each string, count them, and take the maximum at the end.
Algorithm
- Initialize an integer array
cntof length 26 with zeros.
cnt[0]is the number of strings starting with'a'
cnt[25]is the number of strings starting with'z'
- For each string
s:- Extract the first character
s[0] - Compute the index
ord(s[0]) - ord('a')and increment the correspondingcntby 1
- Extract the first character
- Finally, output
max(cnt)
For example, if the input is
apple, art, banana, box, cat
then the counts are a:2, b:2, c:1, and the answer is 2.
Complexity
- Time complexity: \(O(N)\)
(We only process the first character of each string once) - Space complexity: \(O(1)\)
(Only an array of 26 elements. A constant independent of the input size)
Implementation Notes
Since the length of \(S_i\) is at least 1, accessing
s[0]is always safe.There is no need to process the entire string;
s[0]alone is sufficient.Using
sys.stdin.readlinefor fast input ensures safety even when \(N \le 10^5\).Source Code
import sys
def main():
input = sys.stdin.readline
n = int(input().strip())
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.3-codex.
posted:
last update: