<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 solve(vector<string> &ans, stack<char> st,
        int open, int close, int n){
        if(open==close and close==n){
            string op="";
            while(!st.empty()){
                op+=st.top();
                st.pop();
            }
            reverse(op.begin(), op.end());
            ans.push_back(op);
            return;
        }

        if(open<n){
            st.push('(');
            solve(ans, st, open+1, close, n);
            st.pop();
        }

        if(close<open){
            st.push(')');
            solve(ans, st, open, close+1, n);
            st.pop();
        }
    }
    vector<string> generateParenthesis(int n) {
        stack<char> st;
        vector<string> ans;
        int open=0, close=0;
        solve(ans, st, open, close, n);
        return ans;
    }
};
30321