@@ -14,15 +14,24 @@ For example, given n = 3, a solution set is:
14
14
]
15
15
*/
16
16
17
- var generateParentheses = function ( n ) {
18
- if ( n == 0 ) { return [ "" ] } ;
17
+ // ************************************************ Approach1 ************************************************
18
+ var generateParenthesesApproach1 = function ( n ) {
19
+ if ( n === 0 ) { return [ ] } ;
19
20
20
21
var str = "(" . repeat ( n ) ;
21
- var sol = [ ] ;
22
+ sol = [ ] ;
23
+
22
24
genParAux ( str , 0 , 0 , sol )
23
25
return sol ;
24
26
} ;
25
27
28
+ /*
29
+ @param {string } str contains the string generated.
30
+ @param {number } position Current position of the string where new parenthesis would be added.
31
+ @param {string } leftParenthesis Amount for parenthesis left to be added.
32
+ @param {[string] } sol array that contains the solution found so far.
33
+ */
34
+
26
35
var genParAux = function ( str , position , leftParentheses , sol ) {
27
36
if ( position === str . length ) {
28
37
var ret = str + ")" . repeat ( leftParentheses ) ;
@@ -31,7 +40,7 @@ var genParAux = function(str, position, leftParentheses, sol) {
31
40
}
32
41
33
42
genParAux ( str , position + 1 , leftParentheses + 1 , sol ) ; // Don't insert anything
34
- if ( leftParentheses == 0 ) { return ; }
43
+ if ( leftParentheses === 0 ) { return ; }
35
44
36
45
for ( var i = 1 ; i <= leftParentheses ; i ++ ) {
37
46
var parString = ")" . repeat ( i ) ;
@@ -40,15 +49,48 @@ var genParAux = function(str, position, leftParentheses, sol) {
40
49
}
41
50
}
42
51
43
- function main ( ) {
44
- console . log ( "0:" ) ;
45
- console . log ( generateParentheses ( 0 ) ) ;
46
- console . log ( "1:" ) ;
47
- console . log ( generateParentheses ( 1 ) ) ;
48
- console . log ( "2:" ) ;
49
- console . log ( generateParentheses ( 2 ) ) ;
50
- console . log ( "3:" ) ;
51
- console . log ( generateParentheses ( 3 ) ) ;
52
+ // ************************************************ Approach2 ************************************************
53
+ var generateParenthesesApproach2 = function ( n ) {
54
+ if ( n === 0 ) { return [ ] } ;
55
+
56
+ var sol = [ ] ;
57
+ genParAuxApproach2 ( "" , 0 , 0 , 0 , n * 2 , sol )
58
+ return sol ;
59
+ }
60
+
61
+ var genParAuxApproach2 = function ( str , leftPar , rightPar , index , totalCharCount , sol ) {
62
+ if ( index === totalCharCount ) {
63
+ if ( rightPar === leftPar ) {
64
+ sol . push ( str ) ;
65
+ }
66
+ return ;
67
+ }
68
+
69
+ var strLeft = insertAt ( str , index , "(" ) ;
70
+ genParAuxApproach2 ( strLeft , leftPar + 1 , rightPar , index + 1 , totalCharCount , sol ) ;
71
+
72
+ if ( rightPar === leftPar ) { return ; }
73
+
74
+ var strRight = insertAt ( str , index , ")" ) ;
75
+ genParAuxApproach2 ( strRight , leftPar , rightPar + 1 , index + 1 , totalCharCount , sol ) ;
76
+ }
77
+
78
+ var insertAt = function ( str , position , value ) {
79
+ return str . slice ( 0 , position ) + value + str . slice ( position ) ;
52
80
}
53
81
82
+ function main ( ) {
83
+ console . log ( "Approach 1" ) ;
84
+ [ 0 , 1 , 2 , 3 ] . forEach ( function ( elem ) {
85
+ console . log ( `${ elem } : ${ generateParenthesesApproach2 ( elem ) } ` ) ;
86
+ } )
87
+
88
+ console . log ( "-------------" ) ;
89
+
90
+ console . log ( "Approach 2" ) ;
91
+ [ 0 , 1 , 2 , 3 ] . forEach ( function ( elem ) {
92
+ console . log ( `${ elem } : ${ generateParenthesesApproach2 ( elem ) } ` ) ;
93
+ } )
94
+ }
95
+ main ( ) ;
54
96
module . exports . main = main
0 commit comments