Group Anagrams: Optimal Solution (Data-Backed Guide)

By DSA Prep Team · March 4, 2026 · 8 min read · Algorithms

Group Anagrams is the #9 most asked LeetCode problem globally — the most important HashMap + String problem after Two Sum.

Our analysis of 10,385 verified interview questions across 259 companies shows it appeared 91 times across 38 companies — including Google, Amazon, Meta, Microsoft, Bloomberg, Goldman Sachs, Citadel, DoorDash, Expedia, and Intuit.

The problem has two clean solutions — a sorted-key approach (simpler, O(n·k·log k)) and a character-count approach (faster asymptotically, O(n·k)). Know both, understand the tradeoff, and you'll impress any interviewer.

Table of Contents

The Data: 38 Companies Ask This

MetricValue
Total Interview Appearances91 verified mentions
Companies That Ask It38 companies
Global Rank#9 out of 10,385 questions
DifficultyMedium
Core PatternsHash Table, String, Sorting

📌 Companies That Ask This Problem

Google, Amazon, Meta, Microsoft, Bloomberg, Goldman Sachs, Citadel, DoorDash, Expedia, Intuit, Adobe, Anduril, Apple, Atlassian, Autodesk, BlackRock, Cisco, Coupang, Disney, EPAM Systems, Faire, HCL, HashedIn, IBM, Infosys, J.P. Morgan, MSCI, Morgan Stanley, Myntra, NetApp, and 8 more.

Problem Statement

LeetCode #49 — Group Anagrams

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

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

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

Input:  strs = ["a"]
Output: [["a"]]

The Core Intuition: Canonical Keys

Two strings are anagrams if they contain the same characters in the same frequencies. The trick is to find a canonical key — a representation that is identical for all anagrams of a word. Group strings by their canonical key using a hash map.

💡 Two Canonical Key Approaches

Approach 1: Sorted Key — O(n·k·log k)

Sort each string to get its key. Use a defaultdict to group strings with the same sorted key.

Approach 2: Character Count Key — O(n·k)

For each string, build a 26-element frequency tuple. Use it as the hash map key. Faster for long strings since O(k) vs O(k log k).

Code in Python, Java & JavaScript

Python 3 — Sorted Key
from collections import defaultdict

def groupAnagrams(strs: list[str]) -> list[list[str]]:
    groups = defaultdict(list)

    for s in strs:
        key = tuple(sorted(s))  # canonical sorted key
        groups[key].append(s)

    return list(groups.values())
Python 3 — Character Count Key (Faster)
from collections import defaultdict

def groupAnagrams(strs: list[str]) -> list[list[str]]:
    groups = defaultdict(list)

    for s in strs:
        count = [0] * 26
        for c in s:
            count[ord(c) - ord('a')] += 1
        key = tuple(count)  # hashable frequency tuple
        groups[key].append(s)

    return list(groups.values())
Java — Sorted Key
class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();

        for (String s : strs) {
            char[] chars = s.toCharArray();
            Arrays.sort(chars);
            String key = new String(chars);
            map.computeIfAbsent(key, k -> new ArrayList<>()).add(s);
        }

        return new ArrayList<>(map.values());
    }
}
JavaScript — Sorted Key
var groupAnagrams = function(strs) {
    const map = new Map();

    for (const s of strs) {
        const key = s.split('').sort().join('');
        if (!map.has(key)) map.set(key, []);
        map.get(key).push(s);
    }

    return [...map.values()];
};

Complexity Analysis

📊 Complexity Summary

ApproachTimeSpace
Sorted KeyO(n · k log k)O(n · k)
Character Count KeyO(n · k)O(n · k)

Here n = number of strings, k = max string length. Both use O(n·k) space for the output. The character count approach is asymptotically faster but has a larger constant (26 array ops per character vs a sort).

How to Actually Remember This

✅ Your Spaced Repetition Schedule

  1. Today: Solve LC #49 with sorted key. Then implement the character count approach.
  2. Day 3: Solve LC #242 (Valid Anagram) and LC #438 (Find All Anagrams in a String).
  3. Day 7: Solve LC #387 (First Unique Character) — same character count idea.
  4. Day 14: Explain the canonical key concept in one sentence before coding.

Tag on DSAPrep.dev under Hash Table and String.

Track on DSAPrep.dev →