#K75657. Group Anagrams

    ID: 34468 Type: Default 1000ms 256MiB

Group Anagrams

Group Anagrams

Given a list of words, your task is to group the words that are anagrams of each other.

An anagram is a word formed by rearranging the letters of another word (e.g., ate is an anagram of eat). For each group, sort the words in lexicographical order. Then, sort the groups by the lexicographical order of each group's first word, and output each group on a separate line with the words separated by a single space.

Formally, if a word is represented by $w$, its key is defined as the string produced by sorting the characters of $w$ in ascending order. Two words are anagrams if they share the same key. Let each group $G$ be sorted so that $G = [g_1, g_2, \dots, g_k]$ where $g_1 \le g_2 \le \dots \le g_k$, and the list of groups is sorted so that $G_i$ comes before $G_j$ if $g_{i,1} < g_{j,1}$ (where $g_{i,1}$ denotes the first word in group $G_i$).

inputFormat

The input is given from stdin in the following format:

  • The first line contains an integer n ($1 \le n \le 1000$), the number of words.
  • The second line contains n space-separated words. Each word consists of lowercase English letters and has length at most 100.

outputFormat

Print the anagram groups, one group per line. For each group, the words should be sorted in lexicographical order and separated by a single space. The groups themselves should be ordered in ascending order by the first word in each group.

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

bat nat tan

</p>