Skip to content

Commit c6d37af

Browse files
committed
Add week 2 solutions: valid-anagram
1 parent e55594c commit c6d37af

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

valid-anagram/gitsunmin.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* https://leetcode.com/problems/valid-anagram/submissions
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
*/
6+
function isAnagram(s: string, t: string): boolean {
7+
if (s.length !== t.length) return false;
8+
9+
const map = {};
10+
11+
for (const char of s) map[char] = (map[char] ?? 0) + 1;
12+
13+
for (const char of t) {
14+
if (map[char] !== undefined) {
15+
map[char] = map[char] - 1;
16+
} else return false;
17+
}
18+
19+
for (const val of Object.values(map)) if (val !== 0) return false;
20+
21+
return true;
22+
};

0 commit comments

Comments
 (0)