-
Notifications
You must be signed in to change notification settings - Fork 10
/
22_generate-parentheses.js
63 lines (61 loc) · 1.71 KB
/
22_generate-parentheses.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
*
* Problem:
* For a given number N, write a function to generate all combination of ‘N’ pairs
* of balanced parentheses.
* https://leetcode.com/problems/generate-parentheses/
*
* Example 1:
* Input: N=2
* Output: (()), ()()
*
* Example 2:
* Input: N=3
* Output: ((())), (()()), (())(), ()(()), ()()()
*
*
* Time: O(n 2^n)
* Space: O(n 2^n) <- result
*
* @param {number} num
* @return {string[]}
*/
function generateParentheses(num) {
const parentheses = [];
_backtrack(num, ['('], 1, 0, parentheses);
return parentheses;
}
function _backtrack(num, curList, openCount, closeCount, result) {
if (curList.length === 2 * num) {
// O(n) to join array to string
result.push(curList.join(''));
return;
}
/**
* When we construct the `choices` array (see README), we need to consider
* the valid options based on the current `openCount` (count of `(`) and
* `closeCount` (count of `)`):
* 1. if `openCount < num`, we can still append `(`, because the maximum
* `(` can not exceed `num`.
* 2. if `closeCount < openCount`, we can still append `)`, because the
* maximum `)` in any time can't exceed `(`, otherwise it won't be valid
*
* Rough time estimation: every position we have 2 choices, so it would be
* 2^(2n) -> O(2^n)
*/
if (openCount < num) {
curList.push('(');
_backtrack(num, curList, openCount + 1, closeCount, result);
curList.pop();
}
if (closeCount < openCount) {
curList.push(')');
_backtrack(num, curList, openCount, closeCount + 1, result);
curList.pop();
}
}
// Test
console.log(generateParentheses(2));
// [ '(())', '()()' ]
console.log(generateParentheses(3));
// [ '((()))', '(()())', '(())()', '()(())', '()()()' ]