<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> n = 3
<strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> n = 1
<strong>Output:</strong> ["()"]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 8</code></li>
</ul>

class Solution {
    vector<string> genRecurse(string curr, int remO, int remC) {
        // at any point # opening brackets >= # closing brackets
        if (remC == 0) {
            return vector<string>{curr};
        }

        if (remO == remC) {
            return genRecurse(curr + '(', remO - 1, remC);
        }

        if (remO == 0) {
            return vector<string>{curr + string(remC, ')')};
        }

        vector<string> branch1 = genRecurse(curr + '(', remO - 1, remC);
        vector<string> branch2 = genRecurse(curr + ')', remO, remC - 1);
        branch1.insert(branch1.end(), branch2.begin(), branch2.end());

        return branch1;
    }

   public:
    vector<string> generateParenthesis(int n) {
        return genRecurse("", n, n);
    }
};
18475