> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Ransom Note Python Solution with Tests

> Tested Python solution for LeetCode 383 with 11 pytest cases. Generate a practice environment with lcpy.

LeetCode 383, Easy. Topics: Hash Table, String, Counting. [View on LeetCode](https://leetcode.com/problems/ransom-note/description/).

Generate this problem as a practice environment: tested reference solution, 11 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 383   # by problem number
lcpy gen -s ransom_note   # by problem name
```

## Problem

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 `ransomNote`.

### Examples

```
Input: ransomNote = "a", magazine = "b"
Output: false
```

```
Input: ransomNote = "aa", magazine = "ab"
Output: false
```

```
Input: ransomNote = "aa", magazine = "aab"
Output: true
```

### Constraints

* 1 \<= ransomNote.length, magazine.length \<= 10^5
* ransomNote and magazine consist of lowercase English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/ransom_note/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/ransom_note/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(m + n) where m = magazine length, n = ransom_note length
    # Space: O(1) - at most 26 lowercase letters
    def can_construct(self, ransom_note: str, magazine: str) -> bool:
        if len(ransom_note) > len(magazine):
            return False

        magazine_count = Counter(magazine)

        for char in ransom_note:
            if magazine_count[char] == 0:
                return False
            magazine_count[char] -= 1

        return True
```

## Complexity

| Time                                                        | Space                               |
| ----------------------------------------------------------- | ----------------------------------- |
| O(m + n) where m = magazine length, n = ransom\_note length | O(1) - at most 26 lowercase letters |

## Tags

[Grind 75](/catalog/grind-75), [Grind](/catalog/grind), [NeetCode All](/catalog/neetcode).
