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]++;