---
# System prepended metadata

title: \#383 Ransom Note
tags: [leetcode, leetcode-easy]

---

# \#383 Ransom Note
## *給定ransomNote與magazine兩字串, 判斷ransomNote能否用magazine內的字元重新排列組合而成(每個字元只能使用一次但不限定要全部用完)*
## Log
 - build 20210713 by syhuang

## 初見
 - 使用map紀錄magazine內所有字元的出現次數, 然後扣除ransomNote的字元, 字元未出現或是扣到<0就return false
```javascript=
var canConstruct = function(ransomNote, magazine) {
    let map = new Map();
    let result = true;
    for(let i=0; i<magazine.length; i++){
        let letter = magazine[i];
        map.set(letter,map.has(letter)?map.get(letter)+1:1);
    }
    for(let i=0; i<ransomNote.length; i++){
        let letter = ransomNote[i];
        if(!map.get(letter)) return false;
        else{
            let count = map.get(letter);
            if(count==0) return false;
            else map.set(letter,map.get(letter)-1);
        }
    }
    return true;
};
```
## 備註
## 參考
###### tags: `leetcode`, `leetcode-easy`