#K48002. Group Anagrams
Group Anagrams
Group Anagrams
You are given a string containing words separated by spaces. Your task is to group the words that are anagrams of each other. An anagram is formed by rearranging the letters of a word. For each group:
- Sort the words within the group in lexicographical order.
- After forming all groups, sort the groups based on the lexicographical order of their first word.
Finally, print each group on a new line with the words separated by a single space. If the input string is empty, output nothing.
Note: All outputs should be printed to standard output and inputs should be read from standard input.
inputFormat
The input consists of a single line containing space-separated words. It may be empty.
outputFormat
Output each group of anagrams on a new line. In each line, the words (which are anagrams of each other) are sorted in lexicographical order and the groups themselves are ordered by the lexicographical order of their first word.
## samplebat tab rat art tar
art rat tar
bat tab
</p>