Skip to content

Commit 08eaeed

Browse files
authoredOct 1, 2021
Create Generate Parentheses.cpp
1 parent 59c2c54 commit 08eaeed

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ## Question : https://practice.geeksforgeeks.org/problems/generate-all-possible-parentheses/1/?category[]=Recursion&category[]=Recursion&page=1&query=category[]Recursionpage1category[]Recursion#
2+
3+
// ### Solution Video : https://www.youtube.com/watch?v=eyCj_u3PoJE
4+
5+
// SOLUTION : (RECURSIVE AND ACCEPTED)
6+
7+
// ```
8+
class Solution
9+
{
10+
vector<string> v;
11+
void solve(int open,int close,string op){
12+
if(open == 0 && close == 0){
13+
v.push_back(op);
14+
return;
15+
}
16+
17+
if(open!=0){
18+
string op1=op;
19+
op1.push_back('(');
20+
solve(open-1,close,op1);
21+
}
22+
23+
if(close>open){
24+
string op2=op;
25+
op2.push_back(')');
26+
solve(open,close-1,op2);
27+
}
28+
}
29+
30+
public:
31+
vector<string> AllParenthesis(int n)
32+
{
33+
int open=n;
34+
int close=n;
35+
string op ="";
36+
solve(open,close,op);
37+
return v;
38+
}
39+
};

0 commit comments

Comments
 (0)