#C935. Group Anagrams

    ID: 53433 Type: Default 1000ms 256MiB

Group Anagrams

Group Anagrams

Problem Statement

Given a list of words, group the ones that are anagrams of each other. Two words are anagrams if one can be rearranged to form the other (i.e. they contain the same characters in a different order).

You are required to read the input from stdin and print the result to stdout. The first line of input contains an integer \(N\) indicating the number of words. The second line contains \(N\) space-separated words.

Your task is to group the words into anagram groups. For each group, first sort the words in lexicographical order. Then sort the groups by the lexicographical order of their first word. Finally, print each group on a new line with the words separated by a single space.

Example:

Input:
6
eat tea tan ate nat bat

Output: ate eat tea bat nat tan

</p>

Note: If \(N = 0\), output nothing. Ensure your solution covers all edge cases including an empty list and no anagrams.

inputFormat

The input is provided via stdin and consists of the following:

  1. An integer \(N\) on the first line, representing the number of words.
  2. A single line with \(N\) space-separated words.

outputFormat

Output the groups of anagrams via stdout. Each group should be printed on a new line with the words sorted in lexicographical order. The groups themselves must be sorted based on the lexicographical order of their first word.

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

bat nat tan

</p>