#K48002. Group Anagrams

    ID: 28323 Type: Default 1000ms 256MiB

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:

  1. Sort the words within the group in lexicographical order.
  2. 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.

## sample
bat tab rat art tar
art rat tar

bat tab

</p>