Recursion 4 (->generate Brackets Recursion)

why i am getting wrong patters when i am using string s, instead of using char s[1000000].

#include <bits/stdc++.h>
using namespace std;
// char s[1000000];

string s="";

string str(int idx, int n, int a, int b) // a-> no of open brac, b->no of close brackets
{
if(idx == 2*n)
{
// s[idx] = ‘\0’;
cout<< s <<endl;
return “”;
}
if(a<n)
{
// s[idx]=’(’;
s += ‘(’;
str(idx+1,n,a+1,b);
}
if(b<a)
{
// s[idx]=’)’;
s+=’)’;
str(idx+1,n,a,b+1);
}
return “”;
}
int main()
{
int n,idx=0;
cin>>n;
str(idx,n,0,0);
return 0;
}

my output of input 3:

Hey @mukesh_spoj1 share your code using ide.codingblocks.com
And then mention your doubt :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.