Anagram Groups
InterviewTime limit1sMemory limit128 MB
Group distinct words that are anagrams of each other, then print the five largest groups sorted by size and smallest word.
- Level
Medium5 of 10
- Topics
- Hash map, Sorting, String, Implementation
- Solved
- No attempts yet
Problem
Jun-min wants to divide a list of English words into anagram groups.
A word is an anagram of a word if the letters of can be rearranged to form . Words that are anagrams of one another form an anagram group. The size of a group is the number of distinct words it contains; if the exact same word appears several times, it is counted only once.
Given the words, write a program that finds the five largest anagram groups.
Input
The input consists of at most 30,000 lines. Each line contains a single word made up of lowercase letters. The input ends at end of file (EOF).
Output
Print up to the five largest anagram groups. If there are fewer than five groups, print all of them.
Print the groups in order of decreasing size. When two groups have the same size, order them by the lexicographically smallest word in each group, in dictionary order.
Print each group on one line in the form Group of size N: w1 w2 ... wk ., where is the size of the group, followed by the words of the group in dictionary order. Put a single space after each word, and print a single period at the end of the line. Each distinct word is printed only once.