2Brute2Best
🔥0

Group Anagrams

Medium·LeetCode #49 Pro OnlySlight Variation
LeetCode
01

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.

Input

An array of strings strs.

Output

A list of groups (vector<vector<string>>). Every word appears in exactly one group.

Example 1
Input: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
Output: [["bat"], ["nat", "tan"], ["ate", "eat", "tea"]]

"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.

Example 2
Input: strs = [""]
Output: [[""]]

The empty string is still a word. It forms one group by itself.

Example 3
Input: strs = ["a"]
Output: [["a"]]
Constraints
  • • 1 ≤ strs.length ≤ 10^4
  • • 0 ≤ strs[i].length ≤ 100
  • • strs[i] contains only lowercase English letters
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“group the words”
→ Many items need to be split into classes → give each item a key and bucket by key in a hash map.
“anagrams of each other”
→ Order doesn't matter, counts do → sorted word or 26-count signature is the key.
“return in any order”
→ We don't need to keep the input order of groups, so an unordered_map is fine.
03

Approach Complexity Ladder

Brute Force — compare with every groupO(N² · K)O(N · K)

Check each word against the first word of every group with an anagram test

Better — key = sorted wordO(N · K log K)O(N · K)

Sort each word to get its key; group words with the same key in a hash map

Optimal — key = 26-count signatureO(N · K)O(N · K)

Build the key from the 26 letter counts (with separators) in O(K)

Brute Force Intuition

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:

Items that should be grouped together produce the same key (sorted string, count signature). Group by that key.
Remember this · Arrays & Hashing
Trigger
"Have I seen X before?", pairs that add up to a target, grouping things that look alike, counting occurrences.
Think
Instead of scanning again for each element, can I remember earlier elements in a map?
Key question
What exactly do I store as the key, and what as the value?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Arrays & Hashing translate into production architectures and low-level memory mechanics.