File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @description
3+ * time complexity: O(nlogn) split 시 새로운 배열을 생성하고 sort 시 nlogn 시간 소요
4+ * space complexity: O(n) split 시 새로운 배열을 생성함
5+ * runtime: 32ms
6+ * 풀이 방법: 두 문자열을 정렬하여 비교하는 방법
7+ * @param {string } s
8+ * @param {string } t
9+ * @return {boolean }
10+ */
11+ const isAnagram = function ( s , t ) {
12+ return s . split ( "" ) . sort ( ) . join ( "" ) === t . split ( "" ) . sort ( ) . join ( "" ) ;
13+ } ;
14+
15+ /**
16+ * @description
17+ * time complexity: O(n)
18+ * space complexity: O(n)
19+ * runtime: 15ms
20+ * 풀이 방법: 해쉬맵을 통해 카운트를 추가하거나 제거하는 방식, 유니코드도 대응가능
21+ * @param {string } s
22+ * @param {string } t
23+ * @return {boolean }
24+ */
25+ const isAnagramSolution2 = function ( s , t ) {
26+ if ( s . length !== t . length ) return false ;
27+
28+ const map = new Map ( ) ;
29+
30+ for ( let i = 0 ; i < s . length ; i += 1 ) {
31+ map . set ( s [ i ] , ( map . get ( s [ i ] ) || 0 ) + 1 ) ;
32+ map . set ( t [ i ] , ( map . get ( t [ i ] ) || 0 ) - 1 ) ;
33+ }
34+
35+ for ( const value of map . values ( ) ) {
36+ if ( value !== 0 ) return false ;
37+ }
38+
39+ return true ;
40+ } ;
You can’t perform that action at this time.
0 commit comments