From 2b6f7484de2b731e8b03580c45f1681f8fddff5e Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Sat, 22 May 2021 22:40:23 +0800 Subject: [PATCH] =?UTF-8?q?update242.=E6=9C=89=E6=95=88=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 12 +++++++++--- problems/0383.赎金信.md | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 8aa0d171..0c75bbf9 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -143,17 +143,23 @@ func isAnagram(s string, t string) bool { javaScript: ```js +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ var isAnagram = function(s, t) { - const resSet = new Array(25).fill(0); + if(s.length !== t.length) return false; + const resSet = new Array(26).fill(0); const base = "a".charCodeAt(); for(const i of s) { resSet[i.charCodeAt() - base]++; } for(const i of t) { + if(!resSet[i.charCodeAt() - base]) return false; resSet[i.charCodeAt() - base]--; - // if(val < 0) return false; } - return resSet.every(i => !i); + return true; }; ``` diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 7ff28601..755910f9 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -176,7 +176,7 @@ javaScript: * @return {boolean} */ var canConstruct = function(ransomNote, magazine) { - const strArr = new Array(25).fill(0), + const strArr = new Array(26).fill(0), base = "a".charCodeAt(); for(const s of magazine) { strArr[s.charCodeAt() - base]++;