11/**
2- *https://leetcode.com/problems/generate-parentheses
2+ *DFS
33 * Time O(((4^N) / (N * SQRT(N)))) | Space O(((4^N) / (N * SQRT(N))))
44 * Time O(2^N) | Space O(2^N)
5+ * https://leetcode.com/problems/generate-parentheses
56 *@param {number } n
67 *@return {string[] }
78 */
8- var generateParenthesis = ( n ) => dfs ( n ) ; /* Time O(2^N) | Space O(2^N) */
9+ var generateParenthesis = ( n ) => dfs ( n ) ;
910
1011const dfs = ( n , combos = [ ] , open = 0 , close = 0 , path = [ ] ) => {
11- const isBaseCase = path . length === ( n * 2 ) ;
12+ const isBaseCase = ( path . length === ( n * 2 ) ) ;
1213if ( isBaseCase ) {
1314combos . push ( path . join ( '' ) ) ; /* Space O(N + N) */
1415
@@ -25,35 +26,36 @@ const dfs = (n, combos = [], open = 0, close = 0, path = []) => {
2526}
2627
2728const backTrackOpen = ( n , combos , open , close , path ) => {
28- path . push ( '(' ) ; /* | Space O(N) */
29+ path . push ( '(' ) ; /* Space O(N) */
2930dfs ( n , combos , ( open + 1 ) , close , path ) ; /* Time O(2^N) | Space O(2^N) */
3031path . pop ( ) ;
3132}
3233
3334const backTrackClose = ( n , combos , open , close , path ) => {
34- path . push ( ')' ) ; /* | Space O(N) */
35+ path . push ( ')' ) ; /* Space O(N) */
3536dfs ( n , combos , open , ( close + 1 ) , path ) ; /* Time O(2^N) | Space O(2^N) */
3637path . pop ( ) ;
3738}
3839
3940/**
40- *https://leetcode.com/problems/generate-parentheses
41+ *BFS
4142 * Time O(((4^N) / (N * SQRT(N)))) | Space O(((4^N) / (N * SQRT(N))))
4243 * Time O(2^N) | Space O(2^N)
44+ * https://leetcode.com/problems/generate-parentheses
4345 *@param {number } n
4446 *@return {string[] }
4547 */
46- var generateParenthesis = ( n ) => bfs ( n ) ; /* Time O(2^N) | Space O(2^N) */
48+ var generateParenthesis = ( n ) => bfs ( n ) ;
4749
48- const bfs = ( n , queue , combos = [ ] ) => {
50+ const bfs = ( n , combos = [ ] ) => {
4951const queue = new Queue ( [ [ '' , 0 , 0 ] ] ) ;
5052
5153while ( ! queue . isEmpty ( ) ) { /* Time O(2^N) */
5254const [ str , open , close ] = queue . dequeue ( ) ;
53-
54- const isBaseCase = ( open === n ) && ( close === n ) ;
55+
56+ const isBaseCase = ( ( open === n ) && ( close === n ) ) ;
5557if ( isBaseCase ) {
56- combos . push ( str ) ; /* Space O(N) */
58+ combos . push ( str ) ; /* Space O(N) */
5759
5860continue ;
5961}
@@ -69,31 +71,28 @@ const bfs = (n, queue, combos = []) => {
6971}
7072
7173/**
72- *https://leetcode.com/problems/generate-parentheses
74+ *DFS
7375 * Time O(((4^N) / (N * SQRT(N)))) | Space O(((4^N) / (N * SQRT(N))))
7476 * Time O(2^N) | Space O(2^N)
77+ * https://leetcode.com/problems/generate-parentheses
7578 *@param {number } n
7679 *@return {string[] }
7780 */
7881var generateParenthesis = ( n , combos = [ ] ) => {
79- const isBaseCase = n === 0 ;
82+ const isBaseCase = ( n === 0 ) ;
8083if ( isBaseCase ) {
81- combos . push ( '' ) ; /* | Space O(N) */
84+ combos . push ( '' ) ;
8285
83- return combos ;
86+ return combos
8487}
8588
86- return closureNumber ( n , combos ) ; /* Time O(2^N) | Space O(2^N) */
87- }
88-
89- const closureNumber = ( n , combos ) => {
90- for ( let c = 0 ; c < n ; c ++ ) { /* Time O(N) */
91- for ( const left of generateParenthesis ( c ) ) { /* Time O(2^N) | Space O(2^N) */
92- for ( const right of generateParenthesis ( ( ( n - 1 ) - c ) ) ) { /* Time O(2^N) | Space O(2^N) */
93- combos . push ( `(${ left } )${ right } ` ) ; /* | Space O(N) */
89+ for ( let c = 0 ; ( c < n ) ; c ++ ) {
90+ for ( const left of generateParenthesis ( c ) ) {
91+ for ( const right of generateParenthesis ( ( ( n - 1 ) - c ) ) ) {
92+ combos . push ( `(${ left } )${ right } ` ) ;
9493}
9594}
9695}
9796
9897return combos
99- }
98+ }