#K66197. Group Candies by Their Starting Letter
Group Candies by Their Starting Letter
Group Candies by Their Starting Letter
You are given a list of candy names. Your task is to group these candies by the uppercase version of their first letter. The order of candies in each group should remain the same as their original input order, and the groups should be printed in ascending alphabetical order based on the starting letter.
Note: If no candies are provided, your program should not output anything.
The problem can be mathematically modeled as follows. Let \(S = \{s_1, s_2, \dots, s_n\}\) be a set of candy names. For each candy \(s_i\), let \(L(s_i)\) be the uppercase of its first character. Then, group together all candies with the same \(L(s_i)\). Finally, print each group in the order of increasing \(L(s_i)\). Ensure that the relative order of candies within each group is the same as in \(S\).
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer \(n\) (\(0 \le n \le 1000\)), representing the number of candies.
- The following \(n\) lines, each contain a non-empty string representing a candy name. Candy names contain no spaces.
outputFormat
For each group, output one line in the following format:
X: candy1 candy2 ... candy_k
where X
is the uppercase letter representing the first character of the candy names in that group, and candy1, candy2, ..., candy_k
are the candy names in their original input order. The groups must be printed in ascending order by X
. If \(n = 0\), do not output anything.
0