🎉 One-stop destination for all your technical interview Preparation 🎉
Write a function to generate all possible n pairs of balanced parentheses.
balancedParanthesis function. (Main function)
getParanthesis function. (Recursive function)
#include <bits/stdc++.h>
using namespace std;
void getParanthesis(int open, int close, string op, vector<string> &v)
{
if (open == 0 && close == 0)
{
v.push_back(op);
return;
}
if (open != 0)
{
string op1 = op;
op1.push_back('(');
getParanthesis(open - 1, close, op1, v);
}
if (close > open)
{
string op2 = op;
op2.push_back(')');
getParanthesis(open, close - 1, op2, v);
}
}
vector<string> balancedParanthesis(int n)
{
vector<string> v;
int open = n, close = n;
string op = "";
getParanthesis(open, close, op, v);
return v;
}