#C13449. Group Anagrams
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 word. Each input word consists only of lowercase alphabets, and all words have the same length.
Note: In the output, each group should have its words sorted in lexicographical order. Furthermore, the groups themselves should be sorted by the lexicographically smallest word in each group.
For example, if the input is eat tea tan ate nat bat
, then one correct output is:
ate eat tea bat nat tan
inputFormat
The input is read from stdin
and has the following format:
- The first line contains an integer N representing the number of words.
- The following N lines each contain a word.
outputFormat
For each group of anagrams, print a single line containing the words in that group separated by spaces. Within each group, the words must appear in lexicographically sorted order, and the groups themselves must be printed in increasing order based on their smallest word.
## sample6
eat
tea
tan
ate
nat
bat
ate eat tea
bat
nat tan
</p>