<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>

vector<string> generatePermutations(int n)
{
    // Create a vector with n trues and n falses (sorted)
    string permutation(n, '(');
    permutation.insert(permutation.end(), n, ')');
    vector<string> permutations;
    // Generate all permutations
    do
    {
        permutations.push_back(permutation);
    } while (next_permutation(permutation.begin(), permutation.end()));
    return permutations;
}

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> ans;
    auto perms = generatePermutations(n);
    for (auto const &perm : perms)
    {
        stack<bool> left;
        for (auto const &parent : perm)
        {
            if (parent == '(')
                left.push(1);
            else if (left.size())
                left.pop();
            else
                goto invalid;
        }
        ans.push_back(perm);
    invalid:
    }
    return ans;
    }
};
27284