File tree Expand file tree Collapse file tree 2 files changed +72
-0
lines changed Expand file tree Collapse file tree 2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number } n
3+ * @return {number }
4+ */
5+ var climbStairs = function ( n ) {
6+ // 1 -> 1
7+ // 2 -> (1+1) | 2
8+ // 3 -> (1+1+1) | (1+2) | (2+1)
9+ // 4 -> (1+1+1+1) | (2+1+1) | (1+2+1) | (1+1+2) | (1+2+2)
10+ // 5 -> (1+1+1+1+1) | (2+1+1+1) | (1+2+1+1) | (1+1+2+1) | (1+1+1+2) | (1+2+2) | (2+1+2) | (2+2+1)
11+ if ( n <= 3 ) return n
12+
13+ let a = 0 ;
14+ let b = 1 ;
15+ let c = 0 ;
16+
17+ for ( let i = 0 ; i < n ; i ++ ) {
18+ c = a + b ;
19+ a = b ;
20+ b = c ;
21+ }
22+ return b ;
23+ } ;
24+
25+
26+ console . log ( climbStairs ( 2 ) )
27+ console . log ( climbStairs ( 3 ) )
28+ console . log ( climbStairs ( 4 ) )
29+ console . log ( climbStairs ( 5 ) )
30+
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @param {string } t
4+ * @return {boolean }
5+ */
6+ var isAnagram = function ( s , t ) {
7+ if ( s . length !== t . length ) { return false }
8+
9+ // Change string to arr
10+ // let s_arr = s.split('');
11+ // let t_arr = t.split('');
12+ /* WHAT IF SORT IS NOT ALLOWED */
13+ // let s_arr_sort = s_arr.sort();
14+ // let t_arr_sort = t_arr.sort();
15+ // return JSON.stringify(s_arr_sort) === JSON.stringify(t_arr_sort); // Comparison array
16+
17+
18+ // Use map to count characters in the s
19+ // And remove character with t from map
20+
21+ const map = new Map ( ) ;
22+
23+ for ( let c of s ) {
24+ map [ c ] = ( map [ c ] || 0 ) + 1 ;
25+ }
26+
27+ for ( let c of t ) {
28+ if ( ! map [ c ] ) return false ;
29+ map [ c ] -- ;
30+ }
31+
32+ return true ;
33+ } ;
34+ /*
35+ Time Complexity: O(n)
36+ Space Complexity: O(n)
37+ */
38+
39+
40+ console . log ( isAnagram ( "anagram" , "nagaram" ) ) ;
41+ console . log ( isAnagram ( "rat" , "car" ) ) ;
42+
You can’t perform that action at this time.
0 commit comments