File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ // 시간복잡도 O(n * log n)
2+ // 공간복잡도 O(n)
3+
4+ /**
5+ * @param {number } n
6+ * @return {number[] }
7+ */
8+ var countBits = function ( n ) {
9+ const result = [ ]
10+
11+ for ( let i = 0 ; i <= n ; i ++ ) {
12+ const binaryNumber = i . toString ( 2 )
13+ const oneLength = binaryNumber . replaceAll ( '0' , '' ) . length
14+
15+ result . push ( oneLength )
16+ }
17+
18+ return result
19+ } ;
20+
21+ console . log ( countBits ( 5 ) )
Original file line number Diff line number Diff line change 1+ // 시간복잡도: O(n)
2+ // 공간복잡도: O(k)
3+
4+ /**
5+ * @param {string } s
6+ * @param {string } t
7+ * @return {boolean }
8+ */
9+ var isAnagram = function ( s , t ) {
10+ if ( s . length !== t . length ) return false
11+
12+ const checkMap = new Map ( ) ;
13+
14+ for ( let i = 0 ; i < s . length ; i ++ ) {
15+ checkMap . set ( s [ i ] , ( checkMap . get ( s [ i ] ) || 0 ) + 1 )
16+ }
17+
18+
19+ for ( let j = 0 ; j < t . length ; j ++ ) {
20+ if ( ! checkMap . get ( t [ j ] ) || checkMap . get ( t [ j ] ) < 0 ) return false
21+ checkMap . set ( t [ j ] , ( checkMap . get ( t [ j ] ) || 0 ) - 1 ) ;
22+ }
23+
24+ return true
25+ } ;
26+
27+
28+ console . log ( isAnagram ( "anagram" , "nagaram" ) )
You can’t perform that action at this time.
0 commit comments