Group Anagrams
The problem
Partition a list of lowercase English words so each group contains words with identical letter counts. Return the groups and words in any order.
Example
["tea", "ate", "bat", "eat"] → [["tea", "ate", "eat"], ["bat"]]
Need a hint?
Find a signature that every rearrangement of a word shares.
Write pseudocode, trace the example, or note an edge case. This scratchpad does not run code.
Notes stay in this browser when storage is available.
Read the solution approach
Count the 26 letters in each word and use the resulting immutable tuple as a map key. Append the word to that key’s group. Unlike concatenating raw counts without separators, a tuple cannot confuse different frequency vectors. Return the map’s groups.
Complexity
O(total characters + 26n) time and O(total characters + 26n) space including output.
Before moving on, explain why the algorithm is correct and trace a boundary case without looking at the approach.