#C13763. Group Strings by Length
Group Strings by Length
Group Strings by Length
You are given a list of strings. Your task is to group all non-empty strings by their lengths.
For a string s, let \( L = |s| \) be its length. If \( L > 0 \), then include s in the group corresponding to key \( L \). After processing all strings, output each group in a separate line.
The output should be sorted by the group key (i.e. string length) in ascending order, and each line should follow this format:
\( L: s_1\ s_2\ \dots\ s_k \)
If the list of strings is empty or if all strings are empty, output nothing.
inputFormat
The input is given from stdin as follows:
- The first line contains an integer \( n \) representing the number of strings.
- The following \( n \) lines each contain a string. Note that strings may be empty.
outputFormat
For each group of strings with the same length, output a line in the following format:
\( L: s_1\ s_2\ \dots\ s_k \)
where L is the length (group key) and \( s_1, s_2, \dots, s_k \) are the strings in that group in their original input order. The groups must be output in increasing order of \( L \). If there are no valid strings to group, do not output anything.
## sample7
apple
banana
kiwi
pear
mango
cherry
4: kiwi pear
5: apple mango
6: banana cherry
</p>