I am trying to find the maximum palindrome sub-sequence and after going through some tutorials, I came up with a memoized version.But I am not sure about the runtime.I want to know if the following algorithm will work.Could also someone explain what the runtime will be?
Memoized-Palindrome(A,n)
initialize longest [i][j] =0 for all i and j
then return Memoized-Palindrome1(A,1,n,longest)
Memoized-Palindrome1(A,i,j,longest)
if longest[i][j]>0 return longest [i][j]
if (j-i) <=1 return j-i
if A[i]==A[j]
then longest[i][j] = 2 + Memoized-Palindrome1(A,i+1,j-1,longest)
else
longest[i][j]= max(Memoized-Palindrome1(A,i+1,j,longest),Memoized-Palindrome1(A,i,j+1,longest)
return longest[i][j]
j+1
withj-1
, also in the base case you should make a case analysis: ifi == j
and ifj == i + 1
then you have to test whetherA[i] == A[j]
. – jmad Nov 12 '12 at 19:26