Skip to content

Commit 2e7b0ff

Browse files
committed
solve valid anagram
1 parent 5304e39 commit 2e7b0ff

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

valid-anagram/JangAyeon.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
var isAnagram = function (s, t) {
7+
const sCounter = new Map();
8+
for (let e of s) {
9+
// console.log(e)
10+
const value = sCounter.has(e) ? sCounter.get(e) + 1 : 1;
11+
sCounter.set(e, value);
12+
}
13+
console.log(sCounter);
14+
15+
for (let e of t) {
16+
if (!sCounter.has(e)) {
17+
return false;
18+
}
19+
const value = sCounter.get(e);
20+
if (value - 1 < 0) {
21+
return false;
22+
}
23+
if (value - 1 == 0) {
24+
sCounter.delete(e);
25+
} else {
26+
sCounter.set(e, value - 1);
27+
}
28+
}
29+
const notAllused = [...sCounter.keys()].length;
30+
return !notAllused;
31+
};

0 commit comments

Comments
 (0)