Skip to content

Commit bfee821

Browse files
committed
valid-anagram 문제 답안 제출
1 parent b00545f commit bfee821

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

valid-anagram/jjipper.ts

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

0 commit comments

Comments
 (0)