更新242.有效的字母异位词,js方法,采用map

This commit is contained in:
Erincrying
2023-04-11 20:20:33 +08:00
parent ff576a13c2
commit 8f58ab445a

View File

@ -205,6 +205,19 @@ var isAnagram = function(s, t) {
}
return true;
};
var isAnagram = function(s, t) {
if(s.length !== t.length) return false;
let char_count = new Map();
for(let item of s) {
char_count.set(item, (char_count.get(item) || 0) + 1) ;
}
for(let item of t) {
if(!char_count.get(item)) return false;
char_count.set(item, char_count.get(item)-1);
}
return true;
};
```
TypeScript