<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>solve(string s, int n, int o, int e){
        vector<string>res={};
        if(o==e && o==n) {
            res.push_back(s);
            return res;
        }

        vector<string>res1,res2;
        if(o<n){
            string s1 = s+"(";
            res1 = solve(s1,n,o+1,e);
        }
        if(e<n && e<o){
            string s2 = s+")";
            res2 = solve(s2,n,o,e+1);
        }

        for(string s:res1) res.push_back(s);
        for(string s:res2) res.push_back(s);

        
        return res;
    }

    vector<string> generateParenthesis(int n) {
        string s = "";
        int o =0,e=0;
        return solve(s,n,o,e);
    }
};
30018