#C6103. Group Anagrams
Group Anagrams
Group Anagrams
You are given a list of filenames. Two filenames are anagrams if, when their characters are rearranged, they become identical. Your task is to group the filenames that are anagrams of each other.
Each group should be output as a line of space‐separated filenames sorted in lexicographical order. Furthermore, the groups themselves must be ordered in ascending order based on the first filename in each group.
For example, if one group contains listen, silent, inlets
and another contains enlists
, then after sorting each group individually and ordering the groups by their first elements, the output might look like:
bananas enlists google inlets listen silent
Please ensure that your solution reads from standard input and writes to standard output.
inputFormat
The input begins with an integer n (1 ≤ n ≤ 105) representing the number of filenames. Following that are n lines, each containing a single filename composed of lowercase alphabetic characters.
outputFormat
Output the groups of anagrams, one group per line. In each line, print the filenames in that group separated by a single space. The filenames within each group should be sorted in lexicographical order, and the groups should be ordered based on the lexicographical order of their smallest (first) element.
## sample6
listen
silent
enlists
google
inlets
bananas
bananas
enlists
google
inlets listen silent
</p>