File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param s 문자열 1
3+ * @param t 문자열 2
4+ * @returns 두 문자열의 나열을 바꿔 동일한 문자열이 나올 수 있는지 반환
5+ * @description
6+ * - 1. 시간 복잡도: O(n), 공간 복잡도 O(1)
7+ */
8+
9+ // function isAnagram(s: string, t: string): boolean {
10+ // if(s.length !== t.length) {
11+ // return false;
12+ // }
13+ // const sMap = new Map();
14+ // for(let i = 0; i < s.length; i++) {
15+ // sMap.has(s[i]) ? sMap.set(s[i], sMap.get(s[i]) + 1) : sMap.set(s[i], 1);
16+ // sMap.has(t[i]) ? sMap.set(t[i], sMap.get(t[i]) - 1) : sMap.set(t[i], -1);
17+ // }
18+
19+ // for(const v of sMap.values()) {
20+ // if(v) {
21+ // return false;
22+ // }
23+ // }
24+ // return true;
25+ // }
26+
27+ function isAnagram ( s : string , t : string ) : boolean {
28+ if ( s . length !== t . length ) return false ;
29+
30+ const hash : Record < string , number > = { } ;
31+
32+ for ( const letter of s ) {
33+ hash [ letter ] = ( hash [ letter ] || 0 ) + 1 ;
34+ }
35+
36+ for ( const letter of t ) {
37+ if ( hash [ letter ] > 0 ) {
38+ hash [ letter ] = hash [ letter ] - 1 ;
39+ } else {
40+ return false ;
41+ }
42+ }
43+
44+ return true ;
45+ }
You can’t perform that action at this time.
0 commit comments