Skip to content

Commit 5e02d4f

Browse files
committed
solution Valid-Anagram (#218)
#218
1 parent 543c1e7 commit 5e02d4f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

valid-anagram/jiji-hoon96.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,33 @@
22
*
33
* @param s
44
* @param t
5+
*
6+
* 풀이 1
7+
* s.split("").sort().join("") === t.split("").sort().join("") ? true : false
8+
*
9+
* 시간 복잡도: O(n log n)
10+
* 공간 복잡도: O(n)
11+
*
12+
* 너무 비효율적임.. 문자열을 배열로 바꾸고 다시 배열로 변환하고.. 개선해보자
13+
*
514
*/
615

716
function isAnagram(s: string, t: string): boolean {
17+
if(s.length !== t.length) return false;
18+
19+
// 해시맵 만들어주고
20+
const charCount : Record<string,number> = {};
21+
22+
// 여기서는 늘려주고 O(n)
23+
for(let char of s){
24+
charCount[char] = (charCount[char] || 0) + 1;
25+
}
26+
27+
// 여기는 존재하면 없애주자 O(n)
28+
for(let char of t){
29+
if(!charCount[char]) return false;
30+
charCount[char]--;
31+
}
832

33+
return true
934
};

0 commit comments

Comments
 (0)