-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGenerate Parentheses.cpp
46 lines (40 loc) · 1.01 KB
/
Generate Parentheses.cpp
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
/* Leet Code */
/* Title - Generate Parentheses */
/* Created By - Akash Modak */
/* Date - 26/09/2020 */
// Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
// For example, given n = 3, a solution set is:
// [
// "((()))",
// "(()())",
// "(())()",
// "()(())",
// "()()()"
// ]
class Solution {
public:
vector<string> s;
void generate(char *str,int idx,int n,int open, int close){
if(idx==2*n){
str[idx]='\0';
s.push_back(str);
return ;
}
if(open<n){
str[idx]='(';
generate(str,idx+1,n,open+1,close);
}
if(close<open){
str[idx] = ')';
generate(str,idx+1,n,open,close+1);
}
}
vector<string> generateParenthesis(int n) {
if(n==0){
return s;
}
char str[10000000];
generate(str,0,n,0,0);
return s;
}
};