Saturday, 28 September 2013

min no of insertions to make a string palindrome

min no of insertions to make a string palindrome

i was doing this problem in which given a string, i have to perform min no
of insertions to make the string into a palindrome.
i applied recursion.i managed to get the output for the given test cases
but after submitting it i am getting TLE.
what other way should i approach this problem?
example:tada
min no of insertions=1
this is the the function i was using:
int lis(int x,int y)
{
if(x==y)
return 1;
else if(y==x+1)
{
if(a[x]==a[y])
return 2;
else
return 1;
}
else
{
if(a[x]==a[y])
return lis(x+1,y-1)+2;
else
return max(lis(x+1,y),lis(x,y-1));
}
}

No comments:

Post a Comment