---
title: "383. Ransom Note"
url: "https://laigary.com/interview/coding/383-ransom-note"
type: "note"
section: "coding"
date: "2023-11-01"
updated: "2025-10-24"
tags: ["Array", "Hash Table"]
---

# 383. Ransom Note

[383. Ransom Note](https://leetcode.com/problems/ransom-note/)  

```python
class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        counter = Counter(list(magazine))

        for ch in ransomNote:
            if ch in counter:
                if counter[ch] == 0:
                    return False
                counter[ch] -= 1
            else:
                return False
        
        return True
```
```python
class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        r = Counter(ransomNote)
        m = Counter(magazine)
        
        for key, val in r.items():
            if key not in m:
                return False
            if val > m[key]:
                return False
        
        return True
```

時間複雜度 $O(n)$

空間複雜度 $O(n)$
