EASY NC#2 Blind #53 Arrays & Hashing

242. Valid Anagram

๐Ÿ“– Problem

Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

๐Ÿง  Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • โ€ขArray methods (`push`, `pop`, `shift`, `slice`)
  • โ€ขObject/Map/Set usage patterns
  • โ€ขFunction parameter and return typing

Logical Thinking Concepts

  • โ€ขDefine invariants before coding
  • โ€ขCheck edge cases first (`[]`, single element, duplicates)
  • โ€ขEstimate time/space before implementation

๐Ÿ’ก Approach

  • โ†’ Anagrams have same character counts
  • โ†’ Use frequency map to compare
  • โ†’ Sort both strings and compare (alternative)
  • โ†’ Time: O(n log n) for sort, O(n) for frequency map
  • โ†’ Space: O(n) for frequency map, O(n) for sort

๐Ÿ› ๏ธ Hints & Pitfalls

Hints

  • โ€ขAnagrams have same character counts
  • โ€ขUse frequency map to compare
  • โ€ขSort both strings and compare (alternative)

Common Pitfalls

  • โ€ขTime: O(n log n) for sort, O(n) for frequency map
  • โ€ขSpace: O(n) for frequency map, O(n) for sort

๐Ÿงช Test Cases

Hidden tests on submit: 4

Test Case 1
Not run
Input:
isAnagram('anagram', 'nagaram');
Expected:
true
Test Case 2
Not run
Input:
isAnagram('rat', 'car');
Expected:
false
Test Case 3
Not run
Input:
isAnagram('', '');
Expected:
true

๐Ÿ“ Code Editor

๐Ÿ“š Reference Solution

โ–ผ
โŒ˜K Search โŒ˜โ†ฉ Run โŒ˜S Submit