<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>

#include <stack>

class Solution {
public:
    

    void helper(stack<char> s, vector<string> &sol, string curr_str, int i , int n){
        if(i == n){
            for(int j=0; j < s.size(); ++j){
                curr_str.push_back(')');
            }
            sol.push_back(curr_str);
            return;
        }
        else if(s.empty()){
            curr_str.push_back('(');
            s.push('(');
            ++i;
            helper(s, sol, curr_str, i , n);
            return;
        }
        else{
            curr_str.push_back('(');
            s.push('(');
            ++i;
            helper(s,sol,curr_str,i,n);

            curr_str.pop_back();
            curr_str.push_back(')');
            s.pop();
            s.pop();
            --i;
            helper(s, sol, curr_str, i , n);
        }
    }

    vector<string> generateParenthesis(int n) {
        stack<char> s;
        vector<string> sol;
        string curr_str;
        int i = 1;

        s.push('(');
        curr_str = "(";
        helper(s, sol, curr_str, i , n);
        return sol;
    }
};
23943