Full answer
An anagram uses exactly the same characters in a different order, such as "listen" and "silent". Start by asking the rules: ignore case and spaces or not, and which characters are allowed. A common choice is to lowercase the text and remove everything that is not a letter or digit.
Approach 1, sort: [...text].sort().join('') gives a canonical form. Two strings are anagrams when the canonical forms are equal. It is short and easy to read. The cost is O(n log n) for the sort.
Approach 2, count: build a count of each character for the first string, subtract for the second, and check that every count is zero. A quick length check first rejects most non-matches. The cost is O(n) time and O(k) space, where k is the number of different characters. For only lowercase a to z, a fixed array of 26 numbers works and is fast.
Grouping: words that are anagrams share the same canonical form, so use it as a key. Loop over the words, compute the key, and push the word into map.get(key). The result is [...map.values()]. This is O(n * k log k) for n words of length k with the sort key. For very long words, a count signature, such as the counts joined into a string, avoids the sort.
Unicode note: [...text] iterates by code point, which is better than splitting by code unit. Accented letters can have two forms, so call normalize('NFC') first if the input can contain them.
const clean = (text) => text.toLowerCase().normalize('NFC').replace(/[^\p{L}\p{N}]/gu, '');
function isAnagramSort(a, b) {
return [...clean(a)].sort().join('') === [...clean(b)].sort().join('');
}
function isAnagramCount(a, b) {
const x = [...clean(a)];
const y = [...clean(b)];
if (x.length !== y.length) return false;
const counts = new Map();
for (const ch of x) counts.set(ch, (counts.get(ch) ?? 0) + 1);
for (const ch of y) {
const left = counts.get(ch);
if (!left) return false;
counts.set(ch, left - 1);
}
return true;
}
function groupAnagrams(words) {
const groups = new Map();
for (const word of words) {
const key = [...clean(word)].sort().join('');
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(word);
}
return [...groups.values()];
}
console.log(isAnagramSort('Listen', 'Silent'), isAnagramCount('Listen', 'Silent'));
console.log(isAnagramSort('Dormitory', 'Dirty room'), isAnagramCount('Dormitory', 'Dirty room'));
console.log(isAnagramSort('aab', 'abb'), isAnagramCount('aab', 'abb'));
console.log(isAnagramSort('', ''), isAnagramCount('abc', 'abcd'));
console.log(groupAnagrams(['eat', 'tea', 'tan', 'ate', 'nat', 'bat']));
console.log(isAnagramCount('caf\u00e9', 'cafe\u0301'));Run with Node.js v24.22.1 as js-111.js; two runs gave identical output. Output: true true true true false false true false [ [ 'eat', 'tea', 'ate' ], [ 'tan', 'nat' ], [ 'bat' ] ] true
Key points
- Anagrams have the same characters with the same counts; clarify case and spaces first.
- Sort both and compare: simple, O(n log n).
- Count characters with a Map or a 26-slot array: O(n) time.
- Group by using the canonical form as a Map key.
- Use code point iteration and normalize for Unicode text.
Common mistakes
- Forgetting to ignore case or spaces when the question expects it.
- Comparing only lengths or only the set of characters, so "aab" and "abb" pass.
- Sorting with split('') and breaking surrogate pairs in emoji or rare characters.
- Using an object as a counter for arbitrary keys without thinking about inherited names.
Follow-ups
- What is the time complexity of each approach, and when is the count version better?
- How would you find all anagram positions of a word inside a longer text?
- How would you build the group key without sorting?
Tips
- Ask about case, spaces, and character set before coding.
- Offer sort first, then the O(n) count version as the improvement.
- Show grouping with a Map keyed by the canonical form.