Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
class Solution {
public:
vector<string> res;
void parnet(int l, int r, string str){
if(l < 0 || r < 0)
return;
if(l == 0 && r == 0){
res.push_back(str);
return;
}
if(l > 0){
parnet(l-1, r, str+'(');
}
if(r > l){
parnet(l, r-1, str+')');
}
return;
}
vector<string> generateParenthesis(int n) {
int left = n, right = n;
string str = "";
parnet(left, right, str);
return res;
}
};