Learning Outcome
After this lesson, you should be able to convert each word into a canonical key and use that key to group equivalent strings.
Problem Statement
Given an array of strings, group the anagrams together. The order of groups usually does not matter.
| Input | Output | Why |
|---|---|---|
["eat","tea","tan","ate","nat","bat"] | [["eat","tea","ate"],["tan","nat"],["bat"]] | Words with the same character counts are grouped. |
Brute Force Approach
Compare every word with existing groups by checking whether it is an anagram of the group representative.
This repeats anagram checks many times and becomes harder to manage as the input grows.
Optimized Approach
Create a canonical key for each word. A common key is the sorted word. All anagrams produce the same sorted key, so they can be grouped in a hash map.
If the alphabet is fixed, a frequency-count key also works and can avoid sorting each word.
Exact Pseudocode
groups = empty map from key to list
for word in words:
key = sorted characters of word
append word to groups[key]
return all values from groups
Reference Code
class Solution:
def groupAnagrams(self, strs):
groups = {}
for word in strs:
key = "".join(sorted(word))
groups.setdefault(key, []).append(word)
return list(groups.values())Sample Dry Run
| word | key | map action |
|---|---|---|
eat | aet | Create group aet -> [eat] |
tea | aet | Append to aet |
tan | ant | Create group ant -> [tan] |
ate | aet | Append to aet |
Complexity
| Measure | Value | Reason |
|---|---|---|
| Time | O(n * k log k) | There are n words and each word of length k is sorted. |
| Space | O(n * k) | The groups store all words and keys. |
Edge Cases
- Empty string as a word.
- Single-word input.
- Duplicate words should appear in the output group.
Interview Checklist
- Explain the canonical key clearly.
- Mention sorted key versus frequency key tradeoff.
- Do not compare every word pair.
FAQs
Why does sorting form a valid key?
Anagrams have exactly the same characters, so their sorted forms are identical.
Can frequency counts be used instead?
Yes. For lowercase English letters, a 26-count tuple is also a strong key.
What is the core pattern?
Hash map grouping by canonical key.