beginner6 min read·Updated September 19, 2026

Group Anagrams Explained: Grouping ["eat","tea","tan",...]

Master the Group Anagrams problem. Follow a step-by-step walkthrough of ["eat","tea","tan","ate","nat","bat"], build the signature key mental model, and analyze time complexity.

By Learnisim AI·Published September 19, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
  • short strings
Group Anagrams Algorithm: strs = ["eat", "tea", "tan", "ate", "nat", "bat"] 1. Input Word Stream "eat" → sort → "aet" "tea" → sort → "aet" "tan" → sort → "ant" "ate" → sort → "aet" "nat" → sort → "ant" "bat" → sort → "abt" Canonical Signature Key 2. Hash Map (Signature → Buckets) "aet" → "eat" "tea" "ate" (Size: 3) "ant" → "tan" "nat" (Size: 2) "abt" → "bat" (Size: 1) Result: [["eat", "tea", "ate"], ["tan", "nat"], ["bat"]] Core Insight: Anagrams share identical sorted signatures, eliminating pairwise comparison ($O(N \cdot K \log K)$ time).
Group ["eat","tea","tan","ate","nat","bat"] overview diagram
Why

Why Sorting Scrambled Words Is a Mess

Phase 1: The Scrambled List Dilemma

Imagine you are handed a messy box of word cards: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]. Your goal is to tidy them up into piles where every word in a pile is an anagram of the others. Without a smart strategy, you might find yourself constantly comparing every single word against every other word, checking if their letters match. For a tiny list of six words, that is tedious. For a dictionary of thousands of words, doing pairwise comparisons becomes a slow nightmare.
Anagrams share an invisible DNA: they use the exact same letters in different orders. For instance,
Why Sorting Scrambled Words Is a Mess (Group Anagrams) Input: ["eat", "tea", "tan", "ate", "nat", "bat"] — Find the invisible DNA via Canonical Sorting Phase 1: The Unsorted Mess (Pairwise Nightmare) Comparing every word against every other word scales poorly. eat tea tan ate nat bat O(N²) Pairwise Checks? Phase 2: The Canonical DNA (Alphabetize Letters) Sort characters of each word to reveal identical signatures. eat aet tan ant bat abt Key Insight: Anagrams share identical sorted character arrays. Use sorted string as Hash Map Key! O(N log K) Phase 3: Hash Map Grouping (Dictionary of Anagram Buckets) Map Key (Canonical Signature) → List of Original Words "aet" : eat tea ate Grouped in O(N log K) "ant" : tan nat Hash Match Found! "abt" : bat Singletons are valid too
Why Sorting Scrambled Words Is a Mess diagram
Model

The Signature Model: Turning Scrambled Words into Shared Keys

Phase 2: The Signature Model

To group anagrams efficiently without comparing every word against every other word, we need a way to assign them a single, unmistakable identity. In our working example with strs = ["eat","tea","tan","ate","nat","bat"], words like "eat", "tea", and "ate" look completely different on the surface. However, if you sort the letters of each word alphabetically, all three transform into the exact same string: "aet".
This sorted character sequence acts as a signature or bucket key. Instead of treating words as isolated strings, we view them as containers of letter counts. Every word that shares the exact same letter composition produces the identical signature, making it a natural identifier for a hash map.

The Map Structure

We can picture our grouping process as a dictionary where the keys are these sorted signatures and the values are lists of original words that produced them:
- Key "aet" ["eat", "tea", "ate"]
- Key "ant" ["tan", "nat"]
- Key "abt" ["bat"]
By establishing this model, the chaotic task of finding anagrams simplifies into a two-step routine for each word: compute its signature, and drop the original word into the corresponding bucket.
The Signature Model: Turning Scrambled Words into Shared Keys Example input: ["eat", "tea", "tan", "ate", "nat", "bat"] 1. Raw Input Words "eat" "tea" "tan" "ate" "nat" "bat" 2. Sort Letters Compute Signature "eat" → "aet" "tan" → "ant" "bat" → "abt" Same letters = same key! 3. Hash Map Buckets (Signature → Group) KEY "aet" ["eat", "tea", "ate"] KEY "ant" ["tan", "nat"] KEY "abt" ["bat"] Result: Anagrams are grouped instantly by matching sorted signature keys in O(N log K) time.
The Signature Model: Turning Scrambled Words into Shared Keys diagram
Worked example

Tracing the Algorithm: Step-by-Step Execution on the Anagram List

Phase 3: Walking the Trace

Let us trace the algorithm using our locked example: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]. We will maintain a hash map where the keys are canonical signatures (sorted character strings) and the values are lists of original words that share that signature.

Given

strs = ["eat", "tea", "tan", "ate", "nat", "bat"]

Steps

1. First item ("eat"): Sort characters to get signature "aet". Map is currently empty. Insert key "aet" with value ["eat"].
* Map: {"aet": ["eat"]}
2. Second item ("tea"): Sort characters to get signature "aet". Key "aet" already exists. Append "tea" to its list.
* Map: {"aet": ["eat", "tea"]}
3. Third item ("tan"): Sort characters to get signature "ant". Key does not exist. Insert key "ant" with value ["tan"].
* Map: {"aet": ["eat", "tea"], "ant": ["tan"]}
4. Fourth item ("ate"): Sort characters to get signature "aet". Key exists. Append "ate" to its list.
* Map: {"aet": ["eat", "tea", "ate"], "ant": ["tan"]}
5. Fifth item ("nat"): Sort characters to get signature "ant". Key exists. Append "nat" to its list.
* Map: {"aet": ["eat", "tea", "ate"], "ant": ["tan", "nat"]}
6. Sixth item ("bat"): Sort characters to get signature "abt". Key does not exist. Insert key "abt" with value ["bat"].
* Final Map: {"aet": ["eat", "tea", "ate"], "ant": ["tan", "nat"], "abt": ["bat"]}

Result

Extracting all values from the final hash map yields [["eat", "tea", "ate"], ["tan", "nat"], ["bat"]].
Try this: Given the intermediate map state {"aet": ["eat", "tea", "ate"], "ant": ["tan"]}, explain precisely what happens when the word "nat" is processed next.
Phase 3: Walking the Trace (Processing "nat") Interactive Map Trace Example Input Stream strs "eat" → signature: "aet" "tea" → signature: "aet" "tan" → signature: "ant" "ate" → signature: "aet" Fifth Item: "nat" (Current) Signature: "ant" "bat" (Upcoming) Key exists? Append to list! Lookup Key Hash Map State (After "nat") "aet" ["eat", "tea", "ate"] "ant" ["tan", "nat"] Key exists: "nat" appended! "abt" (Pending) [ … ] Key "ant" already exists → Append "nat" to existing list ["tan", "nat"]
Tracing the Algorithm: Step-by-Step Execution on the Anagram List diagram
Apply

Applying the Canonical Signature Pattern to Related String Problems

Phase 4: Applying the Pattern Elsewhere

Now that you have seen how sorting characters into a canonical signature transforms the chaotic list ["eat","tea","tan","ate","nat","bat"] into predictable buckets like ["eat","tea","ate"], ["tan","nat"], and ["bat"], it is time to apply this exact same abstraction elsewhere. The core design pattern here is not just about anagrams—it is about compression through canonical representation. Whenever you have items that look different on the surface but share an underlying structural equivalence, you can design a deterministic function to map them to a shared lookup key.
Consider a neighboring problem: suppose you are given a list of strings and need to group together all words that can be formed by shifting their letters cyclically by some offset (like Caesar cipher shifts, where abc and bcd are equivalent). Just as sorting characters gave us a canonical key for anagrams, what deterministic signature function could you design to group shifted strings, and how would your hash map structure change?

FAQ

How are words like "eat", "tea", and "ate" grouped together in the working example?
By generating a canonical signature for each word—such as sorting its characters alphabetically—"eat", "tea", and "ate" all transform into the signature "aet". The algorithm uses this signature as a hash map key to collect them into the same bucket: ["eat", "tea", "ate"].
What is the time complexity of the hash map signature approach for grouping anagrams?
The time complexity is O(N * K log K), where N is the number of strings and K is the maximum length of a string. Sorting each string takes O(K log K), and we do this for all N strings. The space complexity is O(N * K) to store the hash map buckets.
Why is sorting each string individually better than comparing every word to every other word?
Comparing every word against all other words would result in an inefficient O(N^2 * K) time complexity. Using a hash map with a sorted character signature allows us to classify each string in linear-ithmic time relative to its length.

Keep learning