1143. Longest Common Subsequence

1143. Longest Common Subsequence

class Solution:
    def longestCommonSubsequence(self, text1: str, text2: str) -> int:
        n,m=len(text1),len(text2)
        dp=[[0 for j in range(m+1)] for i in range(n+1)]

        for i in range(n):
            for j in range(m):   
                if text1[i]==text2[j]:
                    dp[i+1][j+1]=dp[i][j]+1
                else:
                    dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1])
                # print(i,j,dp[i+1][j+1])
        return dp[n][m]

make sure the trasition function is correct

你可能感兴趣的:(leetcode)