<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 allParanthesis(int n,string str,vector<char> brackets,int count,vector<string> &ans){
         if(str.length() == 2 * n){
            int count = 0;
            //cout<<str<<endl;
            for(int i=0; i< 2 * n; i++){
                if(count < 0) break;
                if(str[i] == '(') count++;
                else count--;
            }
            if(count == 0)
            ans.push_back(str);
            return;
         }

         for(int i=0; i<2; i++){
            if(str[i] == '(') count++;
            else if(str[i] == ')') count--;
            if(count >= 0){
            str.push_back(brackets[i]);
            allParanthesis(n,str,brackets,count,ans);
            str.pop_back();
            }
            else break;
        }
    }
    vector<string> generateParenthesis(int n) {
        vector<string> ans;
        vector<char> brackets = {'(',')'};
        string str = "";
        int count = 0;
        allParanthesis(n,str,brackets,count,ans);
        return ans;
    }
};
17564