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: