class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
n, m = len(text1), len(text2)
cache = [0] * n
for i in range(m):
for j in range(n):
if j == 0:
cache[j] = 1 if text1[j] == text2[i] else cache[j]
la = cache[j] # 为下一轮备用, 记录左上角的值
else:
tla = cache[j]
cache[j] = la + 1 if text1[j] == text2[i] else max(cache[j], cache[j - 1])
la = tla # 为下一轮备用, 记录左上角的值
return cache[-1]