#C12811. Group Anagrams

    ID: 42280 Type: Default 1000ms 256MiB

Group Anagrams

Group Anagrams

You are given a list of words. Your task is to group the words that are anagrams of each other.

Two words are anagrams if one word's letters can be rearranged to form the other. For example, eat, tea and ate are all anagrams.

Please note that the output should be printed in a standardized format. For each group, sort the words in lexicographical order, and then sort the groups in ascending order based on the first word of each group. Each group must be printed on a new line with its words separated by a single space.

For example, if the input is 6\n eat tea tan ate nat bat, the correct output is:

ate eat tea
bat
nat tan

If there are no words (i.e. n = 0), nothing should be printed.

inputFormat

The input is given in stdin and consists of:

  • An integer n (0 ≤ n ≤ 1000) representing the number of words.
  • If n > 0, a line containing n words separated by spaces.

outputFormat

Print the grouped anagrams to stdout as follows:

  • For each group, print a single line containing the words (in lexicographical order) separated by a single space.
  • The groups themselves should be sorted in lexicographical order based on the first word of each group.
  • If there is no input word (n = 0), print nothing.
## sample
6
eat tea tan ate nat bat
ate eat tea

bat nat tan

</p>