<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 {
private:
    bool isValid(std::string pString) {
        int leftCount = 0;
        for (char p : pString) {
            if (p == '(') {
                leftCount++;
            } else {
                leftCount--;
            }
            if (leftCount < 0) {
                return false;
            }
        }
        return leftCount == 0;
    }

public:
    std::vector<std::string> generateParenthesis(int n) {
        std::vector<std::string> answer;
        std::queue<std::string> queue;
        queue.push("");
        while (!queue.empty()) {
            std::string curString = queue.front();
            queue.pop();
            if (curString.length() == 2 * n) {
                if (isValid(curString)) {
                    answer.push_back(curString);
                }
                continue;
            }
            queue.push(curString + ")");
            queue.push(curString + "(");
        }
        return answer;
    }
};
30625