2020// Space O((2^2n) * n)
2121//
2222// We can generate all 2^2n sequences of '(' and ')' characters. Then, we will check if each one is valid.
23- function generateParenthesis1 ( n ) {
23+ const generateParenthesis1 = ( n ) => {
2424 let res = [ ] ;
2525
26- function generate ( arr ) {
26+ const generate = ( arr ) => {
2727 if ( arr . length === 2 * n ) {
2828 if ( valid ( arr ) ) {
2929 res . push ( arr . join ( '' ) ) ;
@@ -36,50 +36,50 @@ function generateParenthesis1(n) {
3636 generate ( arr ) ;
3737 arr . pop ( ) ;
3838 }
39- }
39+ } ;
4040
41- function valid ( arr ) {
41+ const valid = ( arr ) => {
4242 let bal = 0 ;
43- for ( let c of arr ) {
43+ for ( const c of arr ) {
4444 if ( c === '(' ) bal += 1 ;
4545 else bal -= 1 ;
4646 if ( bal < 0 ) return false
4747 }
4848 return bal === 0 ;
49- }
49+ } ;
5050
5151 generate ( [ ] ) ;
5252 return res ;
53- }
53+ } ;
5454
5555/** 2) Backtracking */
5656// The complexity analysis rests on understanding how many elements there are in generateParenthesis(n). It turns out
5757// this is the n-th Catalan number 1 / n+1 (2n n), which is bounded asymptotically by 4^n / (n * sqrt(n))
5858// Time O(4^n / sqrt(n)). Each valid sequence has at most n steps during the backtracking procedure.
5959// Space O(4^n / sqrt(n)). As described above, and using O(n) space to store the sequence.
6060
61- function generateParenthesis2 ( n ) {
61+ const generateParenthesis2 = ( n ) => {
6262 const res = [ ] ;
6363
64- function go ( l , r , s ) {
64+ const go = ( l , r , s ) => {
6565 if ( s . length === 2 * n ) {
6666 res . push ( s ) ;
6767 return ;
6868 }
6969
7070 if ( l < n ) go ( l + 1 , r , s + '(' ) ;
7171 if ( r < l ) go ( l , r + 1 , s + ')' ) ;
72- }
72+ } ;
7373
7474 go ( 0 , 0 , '' ) ;
7575 return res ;
76- }
76+ } ;
7777
7878/** 3) Backtracking, similar to 2) */
79- function generateParenthesis3 ( n ) {
79+ const generateParenthesis3 = ( n ) => {
8080 const res = [ ] ;
8181
82- function go ( l , r , s ) { // l: left remaining, r: right remaining
82+ const go = ( l , r , s ) => { // l: left remaining, r: right remaining
8383 if ( l > r ) return ; // Check valid by the number of '(' should be always >= ')'
8484
8585 if ( l === 0 && r === 0 ) {
@@ -89,11 +89,11 @@ function generateParenthesis3(n) {
8989
9090 if ( l > 0 ) go ( l - 1 , r , s + '(' ) ;
9191 if ( r > 0 ) go ( l , r - 1 , s + ')' ) ;
92- }
92+ } ;
9393
9494 go ( n , n , '' ) ;
9595 return res ;
96- }
96+ } ;
9797
9898/** 4) Closure number */
9999// Time O(4^n / sqrt(n))
@@ -109,18 +109,18 @@ function generateParenthesis3(n) {
109109//
110110// For each closure number i, we know the starting and ending brackets must be at index 0 and 2*i + 1. Then, the
111111// 2*i elements between must be a valid sequence, plus the rest of the elements must be a valid sequence.
112- function generateParenthesis ( n ) {
112+ const generateParenthesis = ( n ) => {
113113 const res = [ ] ;
114114 if ( n === 0 ) {
115115 res . push ( '' ) ;
116116 } else {
117117 for ( let i = 0 ; i < n ; i ++ ) {
118- for ( let l of generateParenthesis ( i ) ) {
119- for ( let r of generateParenthesis ( n - 1 - i ) ) {
118+ for ( const l of generateParenthesis ( i ) ) {
119+ for ( const r of generateParenthesis ( n - 1 - i ) ) {
120120 res . push ( '(' + l + ')' + r ) ;
121121 }
122122 }
123123 }
124124 }
125125 return res ;
126- }
126+ } ;
0 commit comments