#C12743. Group Anagrams
Group Anagrams
Group Anagrams
You are given a set of n strings. Your task is to group the strings that are anagrams of each other. Two strings are anagrams if by rearranging the letters of one you can get the other. Formally, for two strings s and t, they are anagrams if
\( sort(s) = sort(t) \)
After grouping, each group must be processed as follows:
- Sort the words within each group in lexicographical order.
- Sort the groups in lexicographical order according to the first word of each group.
Print each group on a new line, with words separated by a single space.
inputFormat
The first line of input contains an integer n (0 ≤ n ≤ 105) representing the number of strings. The following n lines each contain a non-empty string composed of lowercase English letters.
outputFormat
For each anagram group, output a single line containing the words in that group separated by a single space. The words within a group should be sorted lexicographically and the groups themselves should be sorted by the lexicographical order of their first word.
## sample6
eat
tea
tan
ate
nat
bat
ate eat tea
bat
nat tan
</p>