#C8157. Group Anagrams

    ID: 52108 Type: Default 1000ms 256MiB

Group Anagrams

Group Anagrams

You are given a list of words. Your task is to group these words into sets of anagrams. Two words are anagrams if one can be rearranged (ignoring case) to form the other. Only groups that contain at least two words should be output.

Input Format:

  • The first line contains an integer \(n\), the number of words.
  • The second line contains \(n\) words separated by spaces.

Output Format:

  • The first line contains an integer \(k\) — the number of anagram groups.
  • This is followed by \(k\) lines, each containing the words of a group separated by a single space. The words in each group should be sorted in a case-insensitive lexicographic order, and the groups themselves should be sorted by the first word in each group (also in a case-insensitive order).

If no anagram groups exist, output 0.

Note: When determining if two words are anagrams, treat uppercase and lowercase letters as equivalent. However, the original case of each word should be preserved in the output.

inputFormat

Standard input begins with an integer (n), the number of words. The next line contains (n) words separated by spaces.

outputFormat

Standard output should begin with an integer (k) denoting the number of anagram groups found. Then, (k) lines follow. Each of these lines contains the words from one group, sorted in case-insensitive lexicographic order and separated by a single space.## sample

7
listen silent enlist rat tar god dog
3

dog god enlist listen silent rat tar

</p>