We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b2a377a commit 021e3f5Copy full SHA for 021e3f5
valid-anagram/sunjae95.js
@@ -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