<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> generatePathens(int n, 
        string &buildingPathen, int index = 0, int openCount = 0) {
        vector<string> results;
        vector<char> chars{ ')'};
        if (openCount < n - index){
            chars.insert(chars.begin(), '(');
        }
        for (int i = 0; i < chars.size(); i++){
            char ch = chars[i];
            buildingPathen[index] = ch;
            if (index == n-1 ){
                results.push_back(buildingPathen);
            } else {
                vector<string> otherResults;
                if (ch == '('){
                    otherResults = 
                    generatePathens(n, buildingPathen, index + 1, openCount + 1);
                } else if (openCount > 0) {
                    otherResults = 
                    generatePathens(n, buildingPathen, index + 1, openCount - 1);
                }
                results.insert(results.end(), 
                    otherResults.begin(), otherResults.end());
            }
        }
        return results;
    }

    vector<string> generateParenthesis(int n) {
        string pathen = "";
        for (int i = 0; i < n*2; i++){
            pathen += ' ';
        }
        return generatePathens(n*2, pathen);
    }
};
24550