#242

Valid Anagram

Easy Frequency count

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether t is a rearrangement of s.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: same letters, same counts?

Why this works: one count array: +1 for s, -1 for t. Anagrams leave all zeros.

Example: "anagram" vs "nagaram"

a: 3-3, n: 1-1, g: 1-1, r: 1-1, m: 1-1 → all 0 → true

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Sorting both strings — works, but O(n log n) instead of O(n).

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(26).
Topic: Strings
🎬 Episodes 16–20: video coming soon