#C8658. Group Anagrams

    ID: 52664 Type: Default 1000ms 256MiB

Group Anagrams

Group Anagrams

Given a list of words, group the words that are anagrams of each other. Two words are anagrams if they contain the same characters in any order, i.e. if \(sorted(word_1)=sorted(word_2)\). Your task is to read the words from standard input and print the groups to standard output.

For each group, sort the words in lexicographical order. Then, sort the groups in ascending order by the lexicographical order of their first word. Each group should be printed on a separate line with words separated by a single space.

inputFormat

The input is read from stdin and consists of two lines:

  • The first line contains an integer \(n\), the number of words.
  • The second line contains \(n\) space-separated words. Note: an empty word is allowed.

outputFormat

Print the grouped anagrams to stdout. Each group should be printed on a new line. Within each line, the words must be sorted lexicographically and separated by a single space. The groups themselves should be ordered by the lexicographical order of their first word.

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

bat nat tan

</p>