In the Leetcode Generate Parentheses problem solution in C++ programming Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Leetcode Generate Parentheses problem solution in C++ programming
class Solution {
public:
vector<string>s;
void gen(string p, int o,int c)
{
if(o == 0 && c == 0)
{
s.push_back(p);
return;
}
if(o>0)
{
gen(p+'(',o-1,c);
}
if(c>0 && c>o)
{
gen(p+')',o,c-1);
}
}
vector<string> generateParenthesis(int n) {
s.clear();
if(n == 0)
{
return s;
}
gen("",n,n);
return s;
}
};
Also read,
- Leetcode Generate Parentheses problem solution in C
- Leetcode Generate Parentheses problem solution in Java
- Leetcode Generate Parentheses problem solution in Python
- Leetcode Generate Parentheses problem solution in C#