File tree Expand file tree Collapse file tree 5 files changed +118
-0
lines changed
longest-common-subsequence
longest-repeating-character-replacement Expand file tree Collapse file tree 5 files changed +118
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * // Definition for a Node.
3+ * function _Node(val, neighbors) {
4+ * this.val = val === undefined ? 0 : val;
5+ * this.neighbors = neighbors === undefined ? [] : neighbors;
6+ * };
7+ */
8+
9+ /**
10+ * @param {_Node } node
11+ * @return {_Node }
12+ */
13+ var cloneGraph = function ( node ) {
14+ if ( ! node ) return null ;
15+
16+ const oldToNew = new Map ( ) ;
17+
18+ const dfs = ( node ) => {
19+ if ( oldToNew . has ( node ) ) {
20+ return oldToNew . get ( node ) ;
21+ }
22+
23+ const copy = new _Node ( node . val ) ;
24+ oldToNew . set ( node , copy ) ;
25+
26+ for ( let neighbor of node . neighbors ) {
27+ copy . neighbors . push ( dfs ( neighbor ) ) ;
28+ }
29+
30+ return copy ;
31+ } ;
32+
33+ return dfs ( node ) ;
34+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } text1
3+ * @param {string } text2
4+ * @return {number }
5+ */
6+ var longestCommonSubsequence = function ( text1 , text2 ) {
7+ const m = text1 . length ;
8+ const n = text2 . length ;
9+
10+ const dp = Array . from ( { length : m + 1 } , ( ) => Array ( n + 1 ) . fill ( 0 ) ) ;
11+
12+ for ( let i = m - 1 ; i >= 0 ; i -- ) {
13+ for ( let j = n - 1 ; j >= 0 ; j -- ) {
14+ if ( text1 [ i ] === text2 [ j ] ) {
15+ dp [ i ] [ j ] = 1 + dp [ i + 1 ] [ j + 1 ] ;
16+ } else {
17+ dp [ i ] [ j ] = Math . max ( dp [ i + 1 ] [ j ] , dp [ i ] [ j + 1 ] ) ;
18+ }
19+ }
20+ }
21+
22+ return dp [ 0 ] [ 0 ] ;
23+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @param {number } k
4+ * @return {number }
5+ */
6+ var characterReplacement = function ( s , k ) {
7+ const count = { } ;
8+ let res = 0 ;
9+ let l = 0 ;
10+ let maxf = 0 ;
11+
12+ for ( let r = 0 ; r < s . length ; r ++ ) {
13+ count [ s [ r ] ] = ( count [ s [ r ] ] || 0 ) + 1 ;
14+ maxf = Math . max ( maxf , count [ s [ r ] ] ) ;
15+
16+ while ( ( r - l + 1 ) - maxf > k ) {
17+ count [ s [ l ] ] -= 1 ;
18+ l += 1 ;
19+ }
20+
21+ res = Math . max ( res , r - l + 1 ) ;
22+ }
23+
24+ return res ;
25+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @return {number }
4+ */
5+ var countSubstrings = function ( s ) {
6+ let res = 0 ;
7+
8+ const countPali = ( s , l , r ) => {
9+ let count = 0 ;
10+ while ( l >= 0 && r < s . length && s [ l ] === s [ r ] ) {
11+ count ++ ;
12+ l -- ;
13+ r ++ ;
14+ }
15+ return count ;
16+ } ;
17+
18+ for ( let i = 0 ; i < s . length ; i ++ ) {
19+ res += countPali ( s , i , i ) ;
20+ res += countPali ( s , i , i + 1 ) ;
21+ }
22+
23+ return res ;
24+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number } n - a positive integer
3+ * @return {number } - a positive integer
4+ */
5+ var reverseBits = function ( n ) {
6+ let res = 0 ;
7+ for ( let i = 0 ; i < 32 ; i ++ ) {
8+ let bit = ( n >>> i ) & 1 ;
9+ res |= ( bit << ( 31 - i ) ) ;
10+ }
11+ return res >>> 0 ;
12+ } ;
You can’t perform that action at this time.
0 commit comments