-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerate Parentheses
44 lines (44 loc) · 1.26 KB
/
Generate Parentheses
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
class Solution {
public:
void solve(string op, int open, int close, vector<string> &ans){
if(open == 0 && close == 0){
ans.push_back(op);
return;
}
//when count of open and close brackets are same then
//we have only one choice to put open bracket
if(open == close){
string op1 = op;
op1.push_back('(');
solve(op1, open-1, close, ans);
}
else if(open == 0){
//only choice is to put close brackets
string op1 = op;
op1.push_back(')');
solve(op1, open, close-1, ans);
}
else if(close == 0){
//only choise is to use open bracket
string op1 = op;
op1.push_back('(');
solve(op1, open-1, close, ans);
}
else{
string op1 = op;
string op2 = op;
op1.push_back('(');
op2.push_back(')');
solve(op1, open-1, close, ans);
solve(op2, open, close-1, ans);
}
}
vector<string> generateParenthesis(int n) {
int open = n;
int close = n;
vector<string> ans;
string op = "";
solve(op, open, close, ans);
return ans;
}
};