Group Anagrams
Problem
Given a list of lowercase words strs, partition them into groups such that every word in a group is an anagram of every other word in the same group. Two words are anagrams when one can be rearranged into the other, using every letter exactly once. Return the groups as a list of lists; the order of groups and the order of words within a group do not matter.
- The list is non-empty and holds at most 25,000 words.
- A word may be empty; no word is longer than 120 letters.
- Only lowercase ASCII letters appear (the 26-slot count key depends on this).
Examples
strs = ["listen", "silent", "enlist", "below", "elbow", "cat"]→[["listen","silent","enlist"], ["below","elbow"], ["cat"]]. Three distinct multisets of letters.strs = ["", "x", ""]→[["", ""], ["x"]]. Two empty words are anagrams of each other.strs = ["zzz"]→[["zzz"]]. One word forms one group.
Hints
- Two words are anagrams exactly when some property of them is identical. What property?
- If you could compute a single value that is the same for all anagrams and different otherwise, grouping would be a one-pass hash-map fill.
- Use the sorted word, or a 26-slot letter count, as the dictionary key.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.