Group anagrams
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
This one builds on a lesson you haven't finished yet. Do that lesson first:
Python Basics · Dictionaries (records) →
Group anagrams
Two words are anagrams if they use exactly the same letters, like eat and tea. Group the anagrams together and return a list of groups.
The trick: sort the letters of each word to make a key — eat and tea both become aet. Words with the same key belong in the same group. Use a dictionary from key → list of words, and remember the order the keys first appear.
Difficulty 4/5 · 280 coins + a small bonus on first completion today
Write group_anagrams(words) that groups words which are anagrams of each other. Return a list of groups: words keep their original order inside a group, and groups are ordered by first appearance.
Click Run to see the output here.