const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Ransom Note LeetCode Solution: Frequency Counting Made Simple

LeetCode 383 is the purest character frequency problem — learn the count-available, subtract-needed pattern that powers anagram and permutation solutions.

6 min read|

Count available, subtract needed — the simplest frequency pattern

LeetCode 383 Ransom Note solved with character frequency counting

Ransom Note: The Purest Frequency Counting Problem

LeetCode 383, Ransom Note, is one of the cleanest introductions to character frequency counting you will find on the platform. The problem asks a simple question: can you construct the ransom note string using only the characters available in the magazine string? Each character in the magazine can only be used once.

This ransom note leetcode problem strips away every distraction and gives you the core mechanic — count what you have, check if it covers what you need. Once you understand this pattern, you can apply it to Valid Anagram (#242), Permutation in String (#567), Group Anagrams (#49), and dozens of other frequency-based problems.

The beauty of this problem is its simplicity. There are no edge cases involving Unicode, no tricky ordering requirements, and no nested data structures. It is just two strings and a question about character availability.

Understanding the Problem

You are given two strings: ransomNote and magazine. Return true if ransomNote can be constructed by using the letters from magazine, and false otherwise. Each letter in magazine can only be used once in constructing the ransom note.

For example, if ransomNote is "aa" and magazine is "aab", the answer is true — the magazine contains two a characters and one b, which is enough to cover the two a characters the ransom note needs. But if ransomNote is "aa" and magazine is "ab", the answer is false — the magazine only has one a, and the ransom note needs two.

Both strings consist only of lowercase English letters. This constraint is important because it means we only need to track 26 possible characters, which makes our frequency map effectively O(1) space regardless of input size.

ℹ️

Pattern Connection

Ransom Note (#383) is the simplest "can I construct X from Y?" problem — the same frequency counting pattern applies to Valid Anagram, Permutation in String, and Group Anagrams.

The Frequency Count Approach

The optimal strategy for this ransom note hash map solution is straightforward: count every character in the magazine, then iterate through the ransom note and subtract each character from the count. If any character count drops below zero, the magazine does not have enough of that character and we return false.

This approach runs in O(m + n) time where m is the length of the magazine and n is the length of the ransom note. We make one pass through the magazine to build the frequency map, then one pass through the ransom note to check availability. Since we only track 26 lowercase letters, the space complexity is O(1).

Why count the magazine first instead of the ransom note? Because we are checking availability. The magazine is the supply, and the ransom note is the demand. By counting the supply first and then subtracting the demand, we immediately know when we run out of any character.

Implementation

The implementation of can construct ransom is remarkably concise. In Python, you can use a Counter from the collections module. In JavaScript or TypeScript, a simple object or Map works perfectly. In Java, an int array of size 26 is the most efficient choice.

The core logic is the same in every language. First, build a frequency map from the magazine string. Then, iterate through each character in the ransom note. For each character, decrement its count in the map. If the count goes below zero at any point, return false immediately — there is no need to check the remaining characters. If you make it through the entire ransom note without any count going negative, return true.

  • Build a frequency map from magazine characters
  • Iterate through ransomNote, decrementing each character count
  • If any count drops below zero, return false immediately
  • If the loop completes without returning false, return true

Visual Walkthrough

Let us walk through two examples to see the frequency counting pattern in action. For ransomNote = "aa" and magazine = "aab", we first count the magazine: {a: 2, b: 1}. Then we process the ransom note: first "a" decrements to {a: 1, b: 1}, second "a" decrements to {a: 0, b: 1}. No count went negative, so we return true.

Now consider ransomNote = "aa" and magazine = "ab". The magazine count is {a: 1, b: 1}. Processing the ransom note: first "a" decrements to {a: 0, b: 1}. Second "a" would decrement to {a: -1, b: 1} — the count went negative, so we immediately return false. The magazine does not have enough a characters.

Notice how the algorithm short-circuits as soon as it detects a shortage. In the worst case we scan both strings completely, but in many practical cases we can exit early when the ransom note demands a character the magazine cannot supply.

⚠️

Avoid This Mistake

Don't sort both strings and compare — that's O(n log n) when frequency counting is O(n). Sorting works but is unnecessarily slow for this type of problem.

Edge Cases

The edge cases for this leetcode 383 solution are minimal, but worth noting. If ransomNote is an empty string, the answer is always true — you need zero characters, and any magazine (even an empty one) satisfies that. If ransomNote is longer than magazine, you can return false immediately as an optimization, since there physically are not enough characters available.

Single-character inputs work naturally with the frequency counting approach. If ransomNote is "a" and magazine is "a", the count goes from 1 to 0 and we return true. If magazine is "b", the character "a" is not found in the map (count is 0 or undefined), so we return false.

  • Empty ransomNote: always true — zero characters needed
  • ransomNote longer than magazine: can return false immediately
  • Single character: handled naturally by the frequency map
  • All same characters: frequency counting handles repeated characters correctly

What This Problem Teaches

Ransom Note is a gateway problem for the entire character frequency check category. The "count available, subtract needed" pattern you learn here is the exact same pattern used in Valid Anagram (#242), where you check if two strings have identical character frequencies. It is also the foundation for Permutation in String (#567) and Find All Anagrams in a String (#438), where you apply frequency counting within a sliding window.

The lesson is simple: whenever a problem asks whether one set of characters can be rearranged, constructed, or matched from another, your first instinct should be frequency counting. Build a map, check the counts, and move on. This ransom note explained walkthrough gives you the simplest version of the pattern so you can recognize it instantly in harder problems.

Practice this pattern until it is automatic. Use YeetCode flashcards to drill the frequency counting template alongside related problems like Group Anagrams (#49) and Minimum Window Substring (#76). When frequency counting becomes second nature, an entire family of string problems becomes trivially solvable.

💡

Pro Tip

Count the magazine characters first, then subtract ransomNote characters — if any count goes below zero, you don't have enough of that character. Simple, clean, O(m+n).

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now