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.
| Metric | Value |
|---|---|
| Total Interview Appearances | 91 verified mentions |
| Companies That Ask It | 38 companies |
| Global Rank | #9 out of 10,385 questions |
| Difficulty | Medium |
| Core Patterns | Hash Table, String, Sorting |
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.
LeetCode #49 — Group Anagrams
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Input: strs = [""]
Output: [[""]]
Input: strs = ["a"]
Output: [["a"]]
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.
"eat" → "aet", "tea" → "aet", "ate" → "aet" — all map to the same key. Simple, O(k log k) per word."eat" → (1,0,0,0,1,0,...,1,...). O(k) per word, no sorting needed.Sort each string to get its key. Use a defaultdict to group strings with the same sorted key.
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).
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())
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())
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());
}
}
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()];
};
| Approach | Time | Space |
|---|---|---|
| Sorted Key | O(n · k log k) | O(n · k) |
| Character Count Key | O(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).
Tag on DSAPrep.dev under Hash Table and String.