update242.有效的字母异位词

This commit is contained in:
qingyi.liu
2021-05-22 22:40:23 +08:00
parent 112832c24d
commit 2b6f7484de
2 changed files with 10 additions and 4 deletions

View File

@ -143,17 +143,23 @@ func isAnagram(s string, t string) bool {
javaScript: javaScript:
```js ```js
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) { 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(); const base = "a".charCodeAt();
for(const i of s) { for(const i of s) {
resSet[i.charCodeAt() - base]++; resSet[i.charCodeAt() - base]++;
} }
for(const i of t) { for(const i of t) {
if(!resSet[i.charCodeAt() - base]) return false;
resSet[i.charCodeAt() - base]--; resSet[i.charCodeAt() - base]--;
// if(val < 0) return false;
} }
return resSet.every(i => !i); return true;
}; };
``` ```

View File

@ -176,7 +176,7 @@ javaScript:
* @return {boolean} * @return {boolean}
*/ */
var canConstruct = function(ransomNote, magazine) { var canConstruct = function(ransomNote, magazine) {
const strArr = new Array(25).fill(0), const strArr = new Array(26).fill(0),
base = "a".charCodeAt(); base = "a".charCodeAt();
for(const s of magazine) { for(const s of magazine) {
strArr[s.charCodeAt() - base]++; strArr[s.charCodeAt() - base]++;