<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:
    void genstr(vector<string> &ans, char str[],int k,int open,int close,int i){
        if(open==k && close==k){
            str[i] = '\0';
            ans.push_back(str);
            return;
        }
        if(open<k){
            str[i] = '(';
            genstr(ans,str,k,open+1,close,i+1);
        }
        if(close<open){
            str[i] = ')';
            genstr(ans,str,k,open,close+1,i+1);
        }
        
    }
    vector<string> generateParenthesis(int n) {
        vector<string> ans;
        int k  = n;
        char str[2*k+1];
        int open = 1;
        int close = 0;
        int i=1;
        str[0] = '(';
        genstr(ans,str,k,open,close,i);
        sort(ans.begin(),ans.end());
        return ans;
    }
};
9363