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:void helper(vector<string> & solution, string & singleSolution,
int numofleft, int numofright, int n){
if (singleSolution.size()==2*n)
solution.push_back(singleSolution);
else{
if (numofleft<n){
singleSolution += '(';
helper(solution, singleSolution, numofleft+1, numofright, n);
singleSolution = singleSolution.substr(0, singleSolution.size()-1);
}
if (numofright<numofleft){
singleSolution += ')';
helper(solution, singleSolution, numofleft, numofright+1, n);
singleSolution = singleSolution.substr(0, singleSolution.size()-1);
}
}
}
vector<string> generateParenthesis(int n) {
vector<string> solution;
string singleSolution = "";
helper(solution, singleSolution, 0, 0, n);
return solution;
}
};