class Solution {
public:
int s[10005];
vector<string> res;
vector<string> generateParenthesis(int n) {
if(n==0)
return res;
fun("",0,0,0,0,n);
return res;
}
void fun(string str,int left,int right,int pos,int index,int n)
{
if(index==n*2)
{
res.push_back(str);
return;
}
if(pos==0)
{
s[pos]=-1;
fun(str+"(",left+1,right,pos+1,index+1,n);
}
else
{
if(s[pos-1]==-1)
{
s[pos]=-1;
if(left<n)
fun(str+"(",left+1,right,pos+1,index+1,n);
if(right<n)
fun(str+")",left,right+1,pos-1,index+1,n);
}
}
}
};