Using backtracking 0 ms sol -

void solve(int n, int op, int cl,string& str, vector<string>& res)
{
/* if number of close braces greater then num of open braces or num of open braces greater
* than n then this is invalid combination return from here*/
if(cl>op || op>n)
return;
if(op+cl==2*n) // if num of open + num of close braces = 2*n then this is valid combination
{
res.push_back(str);
return;
}
// push open brace in str
str.push_back('(');
solve(n,op+1,cl,str,res); // recurse with open+1
str.pop_back(); // backtrack
// push closing brace
str.push_back(')');
solve(n,op,cl+1,str,res); // recurse for close+1
str.pop_back(); // backtrack
}
vector<string> generateParenthesis(int n) {
int op=0,cl=0; // initially number of open and close braces = 0
vector<string> res; // store final result
string str; // store each string combination
solve(n,op,cl,str,res);
return res;
}