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 doGenerate(int x0, int x1, string&& s) {
if (!x0 && !x1) {
m_result.emplace_back(s);
}
if (x0 > 0) {
doGenerate(x0 -1, x1, s + "(");
}
if (x1 > 0 && x1 > x0) {
doGenerate(x0, x1 - 1 , s + ")");
}
}
vector<string> generateParenthesis(int n) {
int x0 = n, x1 = n;
m_result.clear();
doGenerate(n, n, "");
return m_result;
}
private:
vector<string> m_result;
};