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
316. Remove Duplicate Letters - Detailed Explanation
Learn to solve Leetcode 316. Remove Duplicate Letters with multiple approaches.
723. Candy Crush - Detailed Explanation
Learn to solve Leetcode 723. Candy Crush with multiple approaches.
155. Min Stack - Detailed Explanation
Learn to solve Leetcode 155. Min Stack with multiple approaches.
2554. Maximum Number of Integers to Choose From a Range I - Detailed Explanation
Learn to solve Leetcode 2554. Maximum Number of Integers to Choose From a Range I with multiple approaches.
1297. Maximum Number of Occurrences of a Substring - Detailed Explanation
Learn to solve Leetcode 1297. Maximum Number of Occurrences of a Substring with multiple approaches.
2062. Count Vowel Substrings of a String - Detailed Explanation
Learn to solve Leetcode 2062. Count Vowel Substrings of a String 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
$197

Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
$78

Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
$78
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.