The Group Anagrams problem is a popular coding interview question that checks your understanding of string manipulation and hashing techniques. The core idea is to identify and group strings that are anagrams of each other.
In this article, weβll go over the problem statement, walk through two approaches with code, and analyze their time and space complexity.
Given an array of strings strs
, group the anagrams together. You can return the answer in any order.
An anagram is a word or phrase formed by rearranging the letters of another word. For example:
"listen"
and "silent"
"eat"
, "tea"
, "ate"
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["eat","tea","ate"],["tan","nat"],["bat"]]
Sort each wordβs characters alphabetically and use the result as a key in a hash map. All words with the same sorted key are anagrams.
/**
* Groups anagrams by sorting each word.
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function(strs) {
const map = new Map();
for (let word of strs) {
const key = word.split('').sort().join('');
if (!map.has(key)) {
map.set(key, []);
}
map.get(key).push(word);
}
return Array.from(map.values());
};
Time Complexity: O(n * k log k)
n
: number of strings
k
: average length of each string
Sorting takes O(k log k) per string
Space Complexity: O(nk)
Instead of sorting, count the number of each character in the string and use that as the key.
/**
* Groups anagrams using character frequency count as key.
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function(strs) {
const map = new Map();
for (let word of strs) {
const count = new Array(26).fill(0);
for (let char of word) {
count[char.charCodeAt(0) - 'a'.charCodeAt(0)]++;
}
const key = count.join('#'); // use a delimiter to prevent collisions
if (!map.has(key)) {
map.set(key, []);
}
map.get(key).push(word);
}
return Array.from(map.values());
};
Time Complexity: O(n * k)
n
stringsSpace Complexity: O(nk)
Approach | Time Complexity | Pros | Cons |
---|---|---|---|
Sorted Key | O(n * k log k) | Easy to implement | Slower due to sorting |
Character Count | O(n * k) | Faster for large inputs | Slightly more complex |
β Use character count approach for optimal performance, especially with longer strings.
Strings can be empty: [""] β [[""]]
Single characters: ["a"] β [["a"]]
All strings are unique (no anagram pairs): return each in a separate group