Assignment #5: Anagrams

Find large sets of words that are anagrams for each other. Two words are anagrams if they contain the same set of letters (with same frequencies). For instance, least and steal are anagrams. So are steal and stale. In fact, least, steal, tales, stale, and slate are anagrams of each other, and form a large group of anagrams. You must find all large groups (five or more words) of anagrams contained in a dictionary of words: dict.txt (usual disclaimer: it's not my dictionary, and it is not feasible for me to manually purge inappropriate words).

Strategy

For each word, compute its representative. The representative is the characters of the word in sorted order. For instance, the representative for the word enraged is adeegnr. Observe that words that are anagrams will have the same representative. Thus the representative for grenade is also adeegnr. You will use a map in which the key is a string that is a representative, and the value is a vector of all words that have the key as their representative. After constructing the map, you simply need to find all values whose vectors have size five or higher and print those vectors.