DSA Trainer
Frequency MapAI-assisted round

Check if two strings are anagrams

Given two strings, return true if the second is an anagram of the first: same letters, same counts, any order. Return false otherwise.

AIThe assistant proposed this solution

I guard the length first, then count every letter of the first string in a map. For the second string I decrement, and if a letter is missing or already used up I bail out early. I think this is right, but decrement-and-check always makes me a little nervous about off-by-one.

1function isAnagram(s, t) {
2 if (s.length !== t.length) return false;
3 const counts = {};
4 for (const ch of s) counts[ch] = (counts[ch] || 0) + 1;
5 for (const ch of t) {
6 if (!counts[ch]) return false;
7 counts[ch]--;
8 }
9 return true;
10}

Before you accept this, make the call. What's your verdict?