Group Anagrams
Problem Statement
You are given a list of words strs.
Put the words into groups so that two words are in the same group exactly when they are anagrams of each other (same letters, same number of times, any order). Return the list of groups.
The groups can be returned in any order, and the words inside a group can be in any order.
An array of strings strs.
A list of groups (vector<vector<string>>). Every word appears in exactly one group.
"eat", "tea" and "ate" all use one e, one a and one t. "tan" and "nat" use one t, one a and one n. "bat" has no partner, so it is a group on its own.
The empty string is still a word. It forms one group by itself.
- • 1 ≤ strs.length ≤ 10^4
- • 0 ≤ strs[i].length ≤ 100
- • strs[i] contains only lowercase English letters
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Check each word against the first word of every group with an anagram test
Sort each word to get its key; group words with the same key in a hash map
Build the key from the 26 letter counts (with separators) in O(K)
Keep a list of groups. For each new word, compare it with the first word of each group using the Valid Anagram check. If it matches a group, add it there. If it matches none, start a new group.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Arrays & Hashing translate into production architectures and low-level memory mechanics.