383. Ransom Note - Detailed Explanation
Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!
Problem Statement
Given two strings, ransomNote and magazine, determine if you can construct the ransomNote by cutting out letters from the magazine. Each letter in the magazine can only be used once. Return true if it’s possible, otherwise false.
Examples
Example 1
Input: ransomNote = "a", magazine = "b"
Output: false
Explanation: You cannot construct "a" from "b".
Example 2
Input: ransomNote = "aa", magazine = "ab"
Output: false
Explanation: You need two 'a's but magazine has only one.
Example 3
Input: ransomNote = "aa", magazine = "aab"
Output: true
Explanation: You can use the two 'a's from magazine.
Constraints
- 1 ≤ ransomNote.length, magazine.length ≤ 10⁵
ransomNote
andmagazine
consist of lowercase English letters only.
Hints
- You only need to check that every character in the ransom note appears at least as many times in the magazine.
- Can you count letters in the magazine first, then “spend” them as you scan the ransom note?
- Since the alphabet size is fixed (26 letters), a simple array of length 26 can track counts in O(1) per letter.
Approach 1 – Counting Array
Explanation
Use an integer array count[26]
to tally how many times each letter appears in the magazine. Then iterate through each character of the ransom note:
- Convert character to index
i = ch − 'a'
. - Decrement
count[i]
. - If
count[i]
becomes negative, you’ve tried to use a letter more times than the magazine provides → return false.
If you finish without running out of any letter, return true.
Step‑by‑Step
- Initialize
count[0…25] = 0
. - For each
ch
inmagazine
, docount[ch−'a']++
. - For each
ch
inransomNote
, do:idx = ch − 'a'
count[idx]––
- If
count[idx] < 0
, return false.
- Return true.
Complexity
- Time: O(m + n), where m = magazine length, n = ransomNote length
- Space: O(1) (26‑element array)
Approach 2 – Hash Map (Generalized)
Explanation
If the character set weren’t limited to lowercase letters, use a hash map Map<char,int>
to count magazine letters. The logic is identical: build counts, then decrement for each ransom‑note letter, checking for shortages.
Complexity
- Time: O(m + n)
- Space: O(σ), where σ = size of the character set
Python Code
Python3
Python3
. . . .
Java Code
Java
Java
. . . .
Common Mistakes
- Forgetting to return false immediately when a count goes negative.
- Swapping the loops (counting ransom note first) and then checking magazine letters incorrectly.
- Using string operations inside the loop (e.g.
magazine.indexOf
) leading to O(n²).
Edge Cases
- Empty ransomNote (
""
) → always true. - Magazine shorter than ransomNote → can short‑circuit to false.
- All letters in ransomNote are the same, but magazine has exactly one (should be false).
Related Problems
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.