添加383. 赎金信JavaScript版本

This commit is contained in:
qingyi.liu
2021-05-22 11:33:52 +08:00
parent 0a2679ea0b
commit 7699a818b4

View File

@ -136,7 +136,7 @@ class Solution {
```
Python
```
```py
class Solution(object):
def canConstruct(self, ransomNote, magazine):
"""
@ -167,6 +167,28 @@ class Solution(object):
Go
javaScript:
```js
/**
* @param {string} ransomNote
* @param {string} magazine
* @return {boolean}
*/
var canConstruct = function(ransomNote, magazine) {
const strArr = new Array(25).fill(0),
base = "a".charCodeAt();
for(const s of magazine) {
strArr[s.charCodeAt() - base]++;
}
for(const s of ransomNote) {
const index = s.charCodeAt() - base;
if(!strArr[index]) return false;
strArr[index]--;
}
return true;
};
```