Skip to Main Content

Valid Anagram

Problem URL:Valid Anagram

My Solution

JavaScript / TypeScript

const isAnagram = (s: string, t: string): boolean => {
  if (s.length !== t.length) return false;

  const hashTable: Record<string, number> = {};

  for (let char of s) {
    if (char in hashTable) {
      hashTable[char] += 1;
    } else {
      hashTable[char] = 1;
    }
  }

  for (let char of t) {
    if (char in hashTable) {
      hashTable[char] -= 1;
    } else {
      return false;
    }
  }

  return Object.values(hashTable).every((charCount) => charCount === 0);
};

Let's Connect

Twitter GitHub LinkedIn