#C11140. Group Anagrams

    ID: 40424 Type: Default 1000ms 256MiB

Group Anagrams

Group Anagrams

You are given a non-empty string containing space-separated words consisting of lowercase English letters. Your task is to group the words that are anagrams of each other. Two words are anagrams if one can be rearranged to form the other. For each group, sort the words in lexicographical order. Then, sort the groups by comparing their first words in lexicographical order, and finally, output each group on a new line with words separated by a single space.

In mathematical terms, for each word w, let f(w) be the sorted sequence of its characters. All words having the same f(w) belong to one group. Formally, if G is a group then for any two words w1 and w2 in G we have \( f(w_1)=f(w_2)\).

Note that if the input string is empty, the output should be an empty string. If the input contains a single word, simply output that word.

inputFormat

The input consists of a single line read from stdin containing space-separated words. Each word is composed of lowercase English letters. It is possible for the input line to be empty.

outputFormat

Output the grouped anagrams to stdout as follows: Each group is printed on its own line with the words separated by a single space. The words within each group must be sorted in lexicographical order, and the groups themselves must be sorted by their first word in lexicographical order.

## sample
listen silent enlist inlets stop tops pots
enlist inlets listen silent

pots stop tops

</p>