Skip to content

Commit 39011cd

Browse files
committed
valid-anagram
1 parent 416793e commit 39011cd

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

valid-anagram/jun0811.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
7+
// Time Complexity : O(N)
8+
var isAnagram = function (s, t) {
9+
if (s.length != t.length) return false;
10+
11+
const hashMap = {};
12+
13+
for (const chr of s) {
14+
if (chr in hashMap) {
15+
hashMap[chr] += 1;
16+
} else {
17+
hashMap[chr] = 1;
18+
}
19+
}
20+
21+
for (const chr of t) {
22+
if (chr in hashMap && hashMap[chr] > 0) {
23+
hashMap[chr] -= 1;
24+
} else {
25+
return false;
26+
}
27+
}
28+
29+
return true;
30+
};

0 commit comments

Comments
 (0)