Leetcode Generate Parentheses problem solution in C++ programming

In the Leetcode Generate Parentheses problem solution in C++ programming Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

Leetcode Generate Parentheses problem solution in C++ programming

class Solution {
public:

vector<string>s;

void gen(string p, int o,int c)
{
    if(o == 0 && c == 0)
    {
        s.push_back(p);
        return;
    }
    
    if(o>0)
    {
        gen(p+'(',o-1,c);
    }
    
    if(c>0 && c>o)
    {
        gen(p+')',o,c-1);
    }
    
}


vector<string> generateParenthesis(int n) {
    s.clear();
    if(n == 0)
    {
        return s;
    }
    gen("",n,n);
    return s;
}
};

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *