Arrays & Hashing · Easy

Valid Anagram

O(N) · O(1)

Initialize Hash Map

Target is 0. We iterate through nums array and maintain a hash map of { value: index }.

Array Elements & Pointers
1
i=0
2
i=1
3
i=2
Live Variables & Invariants
target:0
i:0
current:-
complement:-
Step 1 / 7
14%
Solution Code
1
function isAnagram(s: string, t: string): boolean {
2
  if (s.length !== t.length) return false;
3
  const count = new Array(26).fill(0);
4
  for (let i = 0; i < s.length; i++) {
5
    count[s.charCodeAt(i) - 97]++;
6
    count[t.charCodeAt(i) - 97]--;
7
  }
8
  return count.every(c => c === 0);
9
}
LeetCode IDE Console

Test Cases

2 cases from Blind 75 & NeetCode 150

Ask for a hint whenever you get stuck.