From 7699a818b42a4cf517553b0cd99b1a3a8ed07f0a Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Sat, 22 May 2021 11:33:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0383.=20=E8=B5=8E=E9=87=91?= =?UTF-8?q?=E4=BF=A1JavaScript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 527945fa..7ff28601 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -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; +}; +```