Skip to content

Commit 2f8a34b

Browse files
committed
feat: 242. Valid Anagram
1 parent bcde25c commit 2f8a34b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

valid-anagram/gwbaik9717.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* @param {string} s
6+
* @param {string} t
7+
* @return {boolean}
8+
*/
9+
var isAnagram = function (s, t) {
10+
if (s.length !== t.length) {
11+
return false;
12+
}
13+
14+
const createDictFromString = (str) => {
15+
const dict = new Map();
16+
17+
for (const chr of str) {
18+
if (dict.has(chr)) {
19+
dict.set(chr, dict.get(chr) + 1);
20+
continue;
21+
}
22+
23+
dict.set(chr, 1);
24+
}
25+
26+
return dict;
27+
};
28+
29+
const dictS = createDictFromString(s);
30+
const dictT = createDictFromString(t);
31+
32+
for (const [key, value] of dictS) {
33+
if (dictT.get(key) !== value) {
34+
return false;
35+
}
36+
}
37+
38+
return true;
39+
};

0 commit comments

Comments
 (0)