1. Length gate
Different lengths can never be anagrams.
if (s.length !== t.length) return falseTyporithm · easy
Step 1 of 8 · intro card
Arrays & Hashing
Tally the first string's characters, decrement with the second; an empty tally means a match.
Anagrams use the exact same characters with the same frequencies.
Counting the first string then decrementing for the second drains the tally if they match.
Any missing character or leftover count means they are not anagrams.
Equal length is required.
Count up, then count down.
Empty tally means anagram.
function isAnagram(s, t) {
if (s.length !== t.length) return false;
const count = new Map();
for (const ch of s) {
count.set(ch, (count.get(ch) ?? 0) + 1);
}
for (const ch of t) {
if (!count.has(ch)) return false;
count.set(ch, count.get(ch) - 1);
if (count.get(ch) === 0) count.delete(ch);
}
return count.size === 0;
}Connect each part of the code to the concept before you type.
Different lengths can never be anagrams.
if (s.length !== t.length) return falseCount each character; ?? handles the first occurrence.
count.set(ch, (count.get(ch) ?? 0) + 1)A character not in the tally means t has an extra.
if (!count.has(ch)) return falseDelete a character once its count balances to zero.
if (count.get(ch) === 0) count.delete(ch)Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.