Home
DSA Patterns
Hash Map
Ransom Note
Ransom Note (LeetCode 383)
Decide whether a ransom note can be built from a magazine's letters by counting the magazine in a hash map and spending one count per note letter.
Problem Understanding
Ransom Note: given strings ransomNote and magazine, return true if the note can be built from the magazine's letters. Each magazine letter can be used only once.
Example: ransomNote = "aa", magazine = "aab" returns true; with magazine = "ab" it returns false, because there is only one a.
The Intuition: A Budget of Letters
Order does not matter, only how many of each letter you have. So treat the magazine as a budget kept in a hash map:
- Count every letter the magazine offers.
- For each letter the note needs, spend one. If its count is already 0, the budget is exhausted: return
false. - If the note is finished, return
true.
It is the same counting idea as Valid Anagram, except that leftover letters are allowed here.
Ransom Note Visualization
COUNT THE MAGAZINE
magazine
0
1
2
ransom note
0
1
letters available
Nothing counted yet
First count every letter the magazine offers
Next
Magazine 'a': 1 available
ransomNote
magazine
The magazine is counted first, then the note spends its letters; the counts still available are beside the figure. Try Example 2 to see the note run out of a.
Ransom Note Dry Run
Input: ransomNote = "aa", magazine = "ab"
| Phase | Letter | Counts after |
|---|---|---|
| count | a | a:1 |
| count | b | a:1, b:1 |
| spend | a | a:0, b:1 |
| spend | a | a is 0: fail |
Answer: false.
Ransom Note LeetCode Solution
A 26-slot count array (Python uses Counter): count the magazine, spend on the note.
Ransom Note Code
1var canConstruct = function(ransomNote, magazine) {2 const counts = new Array(26).fill(0);3 for (const c of magazine) counts[c.charCodeAt(0) - 97]++;4 for (const c of ransomNote) {5 const k = c.charCodeAt(0) - 97;6 if (counts[k] === 0) return false;7 counts[k]--;8 }9 return true;10};
Ransom Note in Python, Java, C++, TypeScript and C
Ransom Note in Python
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
counts = Counter(magazine)
for c in ransomNote:
if counts[c] == 0:
return False
counts[c] -= 1
return TrueRansom Note in Java
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] counts = new int[26];
for (char c : magazine.toCharArray()) counts[c - 'a']++;
for (char c : ransomNote.toCharArray()) {
if (counts[c - 'a'] == 0) return false;
counts[c - 'a']--;
}
return true;
}
}Ransom Note in C++
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int counts[26] = {0};
for (char c : magazine) counts[c - 'a']++;
for (char c : ransomNote) {
if (counts[c - 'a'] == 0) return false;
counts[c - 'a']--;
}
return true;
}
};Ransom Note in TypeScript
function canConstruct(ransomNote: string, magazine: string): boolean {
const counts: number[] = new Array(26).fill(0);
for (const c of magazine) counts[c.charCodeAt(0) - 97]++;
for (const c of ransomNote) {
const k = c.charCodeAt(0) - 97;
if (counts[k] === 0) return false;
counts[k]--;
}
return true;
}Ransom Note in C
bool canConstruct(char* ransomNote, char* magazine) {
int counts[26] = {0};
for (int i = 0; magazine[i]; i++) counts[magazine[i] - 'a']++;
for (int i = 0; ransomNote[i]; i++) {
if (counts[ransomNote[i] - 'a'] == 0) return false;
counts[ransomNote[i] - 'a']--;
}
return true;
}Edge Cases & Common Mistakes
- Note longer than magazine: it can never be built; you can return
falseimmediately as a shortcut. - Reusing a letter: each magazine letter counts once; decrement after every use.
- Extra magazine letters: fine, unlike Valid Anagram.
- Sorting both strings: works but costs O(n log n); counting is linear.
- Checking
count < 0after decrementing: also correct, as long as you check before moving on.
Summary
Count the magazine's letters, then spend one for each letter of the note, failing as soon as a needed letter has a count of 0. O(m + n) time and O(1) space with a 26-slot array.
More practice: DSA interview questions with answers · patterns cheat sheet
