Skip to content

Commit 335b962

Browse files
committed
valid-anagram solution
1 parent 7f70da2 commit 335b962

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

valid-anagram/prgmr99.js

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

0 commit comments

Comments
 (0)