I am unable to find where my code is going wrong.First i calculated LCS of string and it’s reverse than printed ans=length of string - LCS.

Can anyone tell me where am i wrong my code is : here

I am unable to find where my code is going wrong.First i calculated LCS of string and it’s reverse than printed ans=length of string - LCS.

Can anyone tell me where am i wrong my code is : here

I think that you should initialize dp with zeroes and then take care of this:

else cur[j]=0;

If the last two elements are different, it doesn’t mean that the length of the LCS is 0.

I’m not sure if I understood your code right. Why don’t you just take the reversed string and use 2D DP or just DP[from][to] without computing the LCS:

Let dp[i][j] gives us the answer for the sequence S[i],S[i+1],…,S[j].

Then you have two cases:

If S[i]==S[j], then dp[i][j]=dp[i+1][j-1].

If S[i]!=S[j], then dp[i][j]=1+min(dp[i+1][j],dp[i][j-1]).