---
# System prepended metadata

title: ' Leetcode [No.383] Ransom Note (EASY)'
tags: [leetcode]

---

# Leetcode [No.383] Ransom Note (EASY)


## 題目

https://leetcode.com/problems/ransom-note/

## 思路
+ 這個題目算滿常見的，反正就是用26個字母來記錄，如果哪天小於0了就return false;
```c++
class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        int alphabet_array[26] = {};
        for(int i = 0 ; i < magazine.size(); i++)
        {
            alphabet_array[magazine[i]-'a'] ++;
        }
        for(int i = 0 ; i < ransomNote.size(); i++)
        {
            alphabet_array[ransomNote[i]-'a']--;
            if(alphabet_array[ransomNote[i]-'a'] < 0)
            {
                return false;
            }
        }
        return true;
    }
};
```

### 解法分析
+ time complexity: O(nlgn)


### 執行結果

![image](https://hackmd.io/_uploads/BJ8MYMCLp.png)
