<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 {
public:
    vector<string> generateParenthesis(int n) {
        vector<pair<vector<int>, pair<int, int>>>sol;
        sol.push_back({{}, {0,0}});
        for (int i=0; i<(n<<1); i++){
            vector<pair<vector<int>, pair<int, int>>>cpy=sol;
            sol.clear();
            for (auto &pr : cpy){
                if (pr.second.first < n){
                    pr.first.push_back(1);
                    sol.push_back({pr.first, {pr.second.first+1, pr.second.second}});
                    pr.first.pop_back();
                }
                if (pr.second.second < pr.second.first){
                    pr.first.push_back(-1);
                    sol.push_back({pr.first, {pr.second.first, pr.second.second+1}});
                    pr.first.pop_back();
                }
            }
        }
        vector<string> ans;
        for (auto &pr : sol){
            string par = "";
            for (auto &i : pr.first){
                if (i==1){
                    par += '(';
                }else{
                    par += ')';
                }
            }
            ans.push_back(par);
        }
        return ans;
    }
};
12704