Python Solution O(n^2) time and space, using LCS solution


#1
class Solution:
    # @param A : string
    # @return an integer

    def LCS(self, A, B):
        n = len(A)
        m = len(B)
        dp = [[0 for j in range(n+1)] for i in range(m+1)]
    
        for i in range(1,m+1):
            for j in range(1,n+1):
                if A[j-1]==B[i-1] and i!=j: #only change compared to LCS
                    dp[i][j] = 1 + dp[i-1][j-1]        
                else:
                    dp[i][j] = max(dp[i][j-1], dp[i-1][j])
                        
        return(dp[m][n])

    def anytwo(self, A):
        x = self.LCS(A,A)
        if x==0 or x==1:
            return(0)
        
        return(1)