Created
November 9, 2020 02:42
-
-
Save ABHIINAV12/02c0018171d399d3c1fe98709b3e29f1 to your computer and use it in GitHub Desktop.
code for generating all valid parenthesis.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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