Tensorum
LeetCode 383 · Other

Ransom Note

LeetCode 383 · Easy · solution, intuition & step-by-step walkthrough

Counter(magazine) ≥ Counter(note) letter by letter.

EasyHash TableStringCounting
14 msruntime · beats 90%
19.9 MBmemory · beats 8%
1approach
2accepted runs · last 8 Mar 2026

This is how I solved Ransom Note (LeetCode 383), an easy problem from the Other pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.

Intuition

The note can be built if the magazine has at least as many of each letter as the note needs. Count both and compare.

Approach

  1. 1

    Count letters in both strings.

  2. 2

    Any letter needed more often than available → False.

How I solved it

Frequency countsPythonO(m+n) time
class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        freq_ransom = Counter(ransomNote)
        freq_magazine=Counter(magazine)
        for char, freq_ransom_count in freq_ransom.items():
            freq_mag_count = freq_magazine[char]
            if freq_mag_count<freq_ransom_count:
                return False
        return True
Loading the step-through…

Ransom Note — FAQ

What is the intuition behind Ransom Note?

The note can be built if the magazine has at least as many of each letter as the note needs. Count both and compare.

How do you solve Ransom Note (LeetCode 383)?

1. Count letters in both strings. 2. Any letter needed more often than available → False.

What is the time and space complexity of Ransom Note?

Time: O(m + n). Space: O(1) — 26 letters. (Accepted approach: Frequency counts.)

What are common mistakes in Ransom Note?

Each magazine letter can be used once.