Determine whether two strings contain the same characters with the same counts (e.g. 'listen' and 'silent').
Two common approaches: (1) sort both and compare — O(n log n); (2) build a frequency map from the first string and decrement with the second — O(n). Normalize case/whitespace if required.
function isAnagram(a, b) {
if (a.length !== b.length) return false;
const count = {};
for (const ch of a) count[ch] = (count[ch] || 0) + 1;
for (const ch of b) {
if (!count[ch]) return false;
count[ch]--;
}
return true;
}Anagram checks show up in fuzzy matching and in test-data generation where you verify a shuffle preserved all elements.
isAnagram('rat', 'car') should be false but a length-only + sum-of-char-codes check returns true for some inputs. Why is summing char codes wrong?