#383

Ransom Note

Easy Frequency count

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

?The problem

Decide whether the letters of magazine (each used at most once) are enough to write ransomNote.

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

◎Understanding

Really asking: are the magazine's letters enough to write the note?

Why this works: count the magazine's letters and spend them; any count below zero means not enough.

Example: note "aa", magazine "aab"

a: 2 available, spend 2 → ok → true

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using indexOf and replacing characters in strings — slow and messy.

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