Deviopath › Python

Group Anagrams

Medium · Dictionaries

Anagrams are words made of the same letters, like "listen" and "silent". If you sort the letters of each word, anagrams get the same key: "".join(sorted("tea")) is "aet". A collections.defaultdict(list) creates an empty list the first time you use a new key, so you can just append.

Write group_anagrams(words) that groups the words that are anagrams of each other. Sort the words inside each group, then sort the list of groups (lists compare by their first item).

Next: Run-Length Encoding