Skip to content

Instantly share code, notes, and snippets.

@ABHIINAV12
Created November 9, 2020 02:42
Show Gist options
  • Save ABHIINAV12/02c0018171d399d3c1fe98709b3e29f1 to your computer and use it in GitHub Desktop.
Save ABHIINAV12/02c0018171d399d3c1fe98709b3e29f1 to your computer and use it in GitHub Desktop.
code for generating all valid parenthesis.
class Solution {
public:
vector<string> ret;
string s;
void rec(int a,int b){
if(a<0 || b<0 || a>b) return ;
if(a==0 && b==0) {
ret.push_back(s);
return ;
}
s+='(';
rec(a-1,b);
s.pop_back();
s+=')';
rec(a,b-1);
s.pop_back();
}
vector<string> generateParenthesis(int n) {
s=""; rec(n,n);
return ret;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment