LCS similar solution


#1
class Solution:
    def anytwo(self, A):
        dp=[[-1]*(len(A)+1) for i in range(len(A)+1)]
        def func(i,j):
            if i<0 or j<0:
                return 0
            if dp[i][j]!=-1:
                return dp[i][j]
            if i==j:
                dp[i][j]=max(func(i-1,j),func(i,j-1))
            else:
                if A[i]==A[j]:
                    dp[i][j]=func(i-1,j-1)+1
                else:
                    dp[i][j]=max(func(i-1,j),func(i,j-1))
            return dp[i][j]
        return 1 if func(len(A)-1,len(A)-1)>1 else 0