Skip to content

Commit bbe31a4

Browse files
committed
add 242
1 parent 9c2807c commit bbe31a4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

easy/242-valid-anagram.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
var isAnagram = function(s, t) {
7+
if (s === "" || t === "") return true;
8+
if (s.length !== t.length) return false;
9+
10+
const map = {};
11+
for (let i=0; i<s.length; i++) {
12+
const l = s[i];
13+
if (!map[l]) map[l] = 0;
14+
map[l]++;
15+
}
16+
for (let i=0; i<t.length; i++) {
17+
const l = t[i];
18+
if (map[l]) {
19+
map[l]--;
20+
if (map[l] === 0) {
21+
delete map[l];
22+
}
23+
}
24+
}
25+
return !Object.keys(map).length;
26+
};

0 commit comments

Comments
 (0)