Skip to content

Hash maps and sets · routine

Group anagrams

Words are anagrams if they use the same letters: listen and silent. Write anagram_groups(words) returning a list of groups (lists of words, each in original order); the order of the groups does not matter.

Give every word a key that anagrams share — its letters sorted — and group by that key in a dictionary.

Run adds print(anagram_groups(["listen", "silent", "cat"])) after your code, to try it.

Stuck on the idea rather than the code? The Choosing a key lesson walks through it.