<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:
    bool validParanthesis(string& str, int n) {
        
        stack<char> s;
        for (int i = 0; i < 2 * n; i++) {
            char ch = str[i];
            if (ch == '(') {
                s.push(ch);
            } else {
                if (!s.empty() && s.top() == '(') {
                    s.pop();
                } else {
                    return false;
                }
            }
        }

        return s.empty();
    }
    
    void solve(int n,string s,int i,int j,vector<string>& ans){
        if(s.length() == 2*n){
            if(validParanthesis(s,n)){
                ans.push_back(s);
                return;
            }
            else{
                return;
            }
        }
        
        if(i<n){
            solve(n,s+'(',i+1,j,ans);
        }
        if(j<n){
            solve(n,s+')',i,j+1,ans);
        }
    
    }
    
    vector<string> generateParenthesis(int n) {
        int i = 1;
        int j = 0;
        vector<string> ans;
        string s = "(";
        solve(n,s,i,j,ans);
        
        return ans;
    }
};
31536