← Back to DSA Animator
Ransom NoteLC #383EasyHashing · Char Count
Problem

Given two strings ransomNote and magazine, return true if ransomNote can be constructed using the letters from magazine (each letter can be used only once).

Example 1
Input: ransomNote = "aa", magazine = "aab"
Output: true
Constraints: 1≤ransomNote.length,magazine.length≤10⁵  |  Lowercase letters only
Try Examples
Custom:
Approach
Character Frequency Count
Count each letter's frequency in magazine. Then for each letter in ransomNote, decrement that count. If a count goes below 0 → not enough of that letter → return false.
Magazine (source letters)
Ransom Note (to construct)
Letter Counts (from magazine)
Variables
Phase
char
count
result
Step Logic
Press ▶ Play or Next Step to begin.
🎉
Ready
0 / 0
Select an example and press Play.
Algorithm
1
Count each letter's frequency in magazine (int[26])
2
For each letter in ransomNote: decrement its count
3
If count drops below 0 → not enough letters → return false
4
All letters satisfied → return true
Time
O(m+n)
Space
O(1)
Why It Works

We only need letter frequencies — not order. Magazine provides a "budget" of each letter. Each letter used in the ransom note consumes one unit from that budget. If any budget goes negative, we can't build the note.