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