Skip to content

Commit

Permalink
Create GenerateParentheses.cpp
Browse files Browse the repository at this point in the history
Medium
  • Loading branch information
Rahuldandotiya committed Apr 21, 2020
1 parent 81123e2 commit 8fceee3
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions GenerateParentheses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public:
int s;
void generate(vector<string> &ans,int i,int n,int o,int c,string str){
if(c==n){
ans.push_back(str);
return;
}
else{
if(o>c){
str[i]=')';
generate(ans,i+1,n,o,c+1,str);
}
if(o<n){
str[i]='(';
generate(ans,i+1,n,o+1,c,str);
}
}
}
vector<string> generateParenthesis(int n) {
string str="";
for(int i=0;i<2*n;i++) str+=' ';
vector<string> ans;
if(n==0){
ans.push_back("");
return ans;
}
generate(ans,0,n,0,0,str);
return ans;
}
};

0 comments on commit 8fceee3

Please sign in to comment.