Group Anagrams

Practice this coding question on Coding Gurukul.

Problem statement

A word archive needs to group words that contain exactly the same letters with the same frequencies. Keep repeated words as separate entries. Sort the words within each group lexicographically, then sort the groups by their first word. All words are lowercase, so ordinary alphabetical order applies.

Input format

First line: n. Next n lines: one nonempty word per line.

Output format

Print the number of groups. For each group, print its size followed by its sorted words on one line, in the group order specified above.

Constraints

1 <= n <= 10000; 1 <= word length <= 100; total characters <= 100000.

Sample input

6
tea
eat
arc
car
bat
ate

Sample output

3
2 arc car
3 ate eat tea
1 bat

Sign in to run or submit a solution in C++, Java or Python.