<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> generateParenthesis(int n) {
        std::vector<std::string> temp{};
        temp.push_back("");

        for(int i = 0; i < 2 * n; ++i) {
            std::vector<std::string> collect{};

            for(auto& str: temp) {
                auto copy = str;
                str += "(";
                copy += ")";

                collect.push_back(copy);
            }
            for(auto& str: collect)
                temp.push_back(std::move(str));
        }
        std::vector<std::string> collect{};

        for(auto& str: temp) {
            int count = 0;

            bool add = true;

            for(auto ch: str) {
                count += ch == '(' ? 1 : -1;
                if (count < 0) {
                    add = false;
                    break;
                }
            }

            if(add && count == 0)
                collect.push_back(str);
        }

        return collect;
    }
};
28195