Group Anagrams - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

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.

Time Complexity

Let n be the number of input words and m be the maximum length of a single word.

  • For each word: We build a 26-length frequency array by scanning its characters once. This takes O(m) time.
  • Build the key: We join the 26 counts into a comma-separated string. This is O(26) = O(1) per word.
  • Insert in the map: Looking up and updating the hash map with that key is O(1) on average.

We repeat this for all n words, so the total runtime is O(n · m).

Space Complexity

Let n be the number of input words and m be the average length of each word. The algorithm’s space usage grows in proportion to the total input size, or O(n · m).

  • Grouping structure: Each word is stored exactly once in the hash map under its frequency key → O(n · m) total space.
  • Key representation: Each word’s frequency key has a fixed size of 26 integers (for 'a'–'z'), so total key storage adds only O(n) space.
  • Temporary workspace: A constant-size (26-slot) frequency array is built for each word during processing → O(1) per iteration.

In summary: The overall space complexity is O(n · m), dominated by storing all input words and their groupings in the map.

Visucodize editorial solution titled JS Solution for LeetCode's Group Anagrams coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/group-anagrams.