383. Ransom Note - Detailed Explanation
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⁵
ransomNoteandmagazineconsist 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
chinmagazine, docount[ch−'a']++. - For each
chinransomNote, 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
3119. Maximum Number of Potholes That Can Be Fixed - Detailed Explanation
Learn to solve Leetcode 3119. Maximum Number of Potholes That Can Be Fixed with multiple approaches.
344. Reverse String - Detailed Explanation
Learn to solve Leetcode 344. Reverse String with multiple approaches.
381. Insert Delete GetRandom O(1) - Duplicates allowed - Detailed Explanation
Learn to solve Leetcode 381. Insert Delete GetRandom O(1) - Duplicates allowed with multiple approaches.
1404. Number of Steps to Reduce a Number in Binary Representation to One - Detailed Explanation
Learn to solve Leetcode 1404. Number of Steps to Reduce a Number in Binary Representation to One with multiple approaches.
523. Continuous Subarray Sum - Detailed Explanation
Learn to solve Leetcode 523. Continuous Subarray Sum with multiple approaches.
341. Flatten Nested List Iterator - Detailed Explanation
Learn to solve Leetcode 341. Flatten Nested List Iterator with multiple approaches.
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.
4.6
(69,299 learners)
$197
New

Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
(1,107 learners)
$78
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
(26,683 learners)
$78
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.