que link - - LeetCode
class Solution {
public:
vector<string>st;
void recur(int oc,int cc,int n,string s)
{
cout<<s<<" "<<oc<<" "<<cc<<endl;
if((oc + cc) == (n*2))
{
st.push_back(s);
}
if(oc<n)
{
recur(oc+1,cc,n,s+'(');
}
if(cc<oc)
{
recur(oc,cc+1,n,s+')');
}
cout<<s<<" "<<oc<<" "<<cc<<endl;
}
vector<string> generateParenthesis(int n) {
recur(0,0,n,"");
return st;
}
};
My doubt is we will insert ( till oc is less than n now we will add ) till cc less than oc
the recursion debug looks like this
(
((
(((
((()
((())
((()))
here is my doubt it will backtrack and cc will become 2 now why it wont add another ) and append to the result why it is going till ((( here.