Easy Problems

242. Valid Anagram

What will be learned

  • Explore how hash tables can be used to compare character frequencies efficiently.

Solutions

function isAnagram(s: string, t: string): boolean {
  if (s.length !== t.length) return false;
 
  const map = {};
  for (let i = 0; i < s.length; i++) { 
      map[s[i]] = (map[s[i]] || 0) + 1;
      map[t[i]] = (map[t[i]] || 0) - 1;
  }
 
  for(let key in map) {
      if(map[key] !== 0) return false
  }
 
  return true;
};

Video Solution

Last Update: 12:08 - 16 April 2024
Hash Table
String
Sorting

On this page