algorithm to group all anagrams from a dictionary

I recently came accross this interesting problem of grouping all anagrams from a huge dictionary in a very efficient way.

Without giving too much thoughts, I came accross the following algo:

- let hashset = new Hash Set Data Structure
- for each word in the dictionary at position x, do
– let arranged-word = rearrange-letters-of-the-word-in-alphabetical-order(word)
– if arranged-word already exists in the hashset as a key, then
– update current-value against that key to current-value concatenated with comma & position x
– else insert the arranged-word as a key in the hashset with value as position x
- end do

As a result of running this algo, all anagram positions in the dictionary will be stored as values in the hash set.

Let me know if anyone has a better solution to this problem.


About this entry