#K63157. Group Anagrams

    ID: 31691 Type: Default 1000ms 256MiB

Group Anagrams

Group Anagrams

You are given a list of words, and your task is to group the words that are anagrams of each other. Two words are anagrams if their letters can be rearranged to form each other. For each group, you must:

  • Sort the words within the group in lexicographical order.
  • Then, sort the groups in increasing order based on the lexicographically smallest word in each group.

If there are no words (i.e. n = 0) then nothing should be printed. All input should be read from standard input (stdin) and the result should be output to standard output (stdout).

Note: The output should list each anagram group in a separate line, with the words separated by a single space.

inputFormat

The first line of input contains an integer n (n ≥ 0) representing the number of words. The second line contains n words separated by spaces.

For example:

6
eat tea tan ate nat bat

outputFormat

Output the groups of anagrams, one group per line. Within each line, the words must be sorted lexicographically and separated by a single space. The groups themselves should be sorted in increasing order based on the smallest word in each group.

## sample
6
eat tea tan ate nat bat
ate eat tea

bat nat tan

</p>