更新242.有效的字母异位词,js方法,采用map
This commit is contained in:
parent
ff576a13c2
commit
8f58ab445a
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Reference in New Issue