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

/*
n -> ( [n-2] ) or () [n-2]

*/
class Solution {
public:
    map<int, vector<string>> M;
    vector<string> generateParenthesis(int n) {
        if(M.count(n)){return M[n];}
        if(n==1){return {"()"};}
        set<string> S;
        vector<string> ret;
        for(int i = 1; i<n; ++i){
            auto s1 = generateParenthesis(i);
            auto s2 = generateParenthesis(n-i);
            for(auto& u:s1){
                for(auto& v:s2){
                    S.insert(u+v);
                }
            }
        }
        auto s3 = generateParenthesis(n-1);
        for(auto& u:s3){
            S.insert("("+u+")");
        }
        for(auto& e:S){
            ret.push_back(e);
        }
        return M[n] = ret;
    }
};
11489