Back to problems Solve on LeetCode →

Group Anagrams

LeetCode 49 • Medium • Arrays & Hashing

Group strings that are anagrams. Key = sorted(s). Iterative: hash map in one pass. Recursive: group rest, merge first.

TimeO(n·k log k)n strings, sort each k chars
SpaceO(n)groups dict
i: key:
Current string
Group key
Grouped
key
groups
Ready
Press Play. For each string: key = sorted(s), append to groups[key]. Anagrams share the same key.