<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 paren(string x){
        stack<char> s;
        for(int i =0; i<x.size(); i++){
            if(x[i]=='('){
                s.push(x[i]);
            }
            else if(x[i]==')'){
                if(s.empty()) {
                    return false;
                }
                s.pop();
            }
            
        }
        return s.empty();
    }
    vector<string> generateParenthesis(int n) {
        string a;
        for(int i=0; i<n; i++){
            a.push_back('(');
            
        }
        for(int i=0; i<n; i++){
            a.push_back(')');
            
        }
        vector<string> x;
        do{
            cout<<a<<endl;
            if(paren(a)){
                
                x.push_back(a);
            }
            
        }while(next_permutation(a.begin(),a.end()));
        return x;
        
        
    }
};
26069