The key insight for Grouping Anagrams is that all anagrams share the same letter composition — they contain exactly the same letters with the same frequencies, but possibly in a different order. Therefore, to group anagrams, we need a way to represent this composition uniquely for each word.
A straightforward approach is to sort each word alphabetically and use the sorted string as a key.
However, sorting each word takes O(m log m) time (where m is the word’s length), which can be costly for large inputs.
Instead, we use a more direct method — a frequency-based key that captures the count of each letter efficiently in a single linear pass.
In this optimized approach, every word is represented by a 26-length frequency array (for 'a'–'z'),
where each index corresponds to a letter’s count.
The array is then joined into a comma-separated string (e.g. "1,0,2,0,...")
to serve as a unique key in a HashMap.
All anagrams produce the same frequency key, so they are automatically grouped together.
- Step 1 — Build frequency key:
For each word, compute its 26-length frequency array and convert it into a string key using
array.join(','). - Step 2 — Group words:
Store each word under its frequency key in a
HashMap. Words sharing identical keys have identical character frequencies → they’re anagrams. - Step 3 — Output: Return the map’s values — each list represents one anagram group.
Why this works: Two words are anagrams if and only if they have identical character frequency distributions. Using the frequency key ensures that all anagrams share the same key, while non-anagram words always produce distinct keys.