Feb 2, 2009

Given n pairs of parentheses. Write a program to print out all valid configrations. For example n=2, ()(), (()) are valid, but ))((, ())( are not.

Given n pairs of parentheses. Write a program to print out all valid configrations.
For example n=2, ()(), (()) are valid, but ))((, ())( are not.
foo(stack s, int count, int len)
{
if(count<0)
return ;
if(len==0)
{
if(count==0)
print s;
else return;
}
foo(s.push('('),count+1,len-1);
foo(s.push(')'),count-1.len-1);
}

No comments:

Post a Comment