Skip to content

Commit 021e3f5

Browse files
committed
Valid Anagram
1 parent b2a377a commit 021e3f5

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

valid-anagram/sunjae95.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @description
3+
* time complexity: O(N)
4+
* space complexity: O(N)
5+
*
6+
* brainstorming:
7+
* 1. hash table value compare to count
8+
*
9+
* strategy:
10+
* string change to hash table
11+
*/
12+
var isAnagram = function (s, t) {
13+
if (s.length !== t.length) return false;
14+
15+
let answer = true;
16+
const tableS = convertHashTable(s);
17+
const tableT = convertHashTable(t);
18+
19+
tableS.forEach((_, key) => {
20+
if (tableT.get(key) && tableT.get(key) === tableS.get(key)) return;
21+
answer = false;
22+
});
23+
24+
return answer;
25+
};
26+
27+
const convertHashTable = (str) =>
28+
str.split("").reduce((map, s) => {
29+
map.set(s, (map.get(s) ?? 0) + 1);
30+
return map;
31+
}, new Map());

0 commit comments

Comments
 (0)