<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:
    vector<string> results = {};
    void append(string s , string a , int n) {
        s += a; // add the new character
            int count = 0;
        for (string::iterator it = s.begin(); it != s.end(); it++)
            {
            if (*it == '(') count++;
            if (*it == ')'){
                count--;
                // if less than 1 we have too many ) so break recursion
                if (count < 0) return;
                }
            }
        if (s.length() < n * 2)
            {  //check that we're not too long
            append(s , "(" , n);  //recurse the 2 results
            append(s , ")" , n);
            }
        else if (!count) results.push_back(s); // check validity
            
            // int count = 0;
            // for (string::iterator it = s.begin(); it != s.end(); it++)
            //     {
            //     if (*it == '(') count++;
            //     if (*it == ')'){
            //         count--;
            //         // if less than 1 we have too many ) so break recursion
            //         if (count < 0) return;
            //         }
            //     }
            //if were at 0, and never hit -1 then its good, add to results
            
        
        }//append
    vector<string> generateParenthesis(int n) {
        append("" , "(" , n);
        return results;

        }
    };
20601