# Subject Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0. A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters. For example, "ace" is a subsequence of "abcde". A common subsequence of two strings is a subsequence that is common to both strings. ade abe => ae ![](https://i.imgur.com/F11I1nI.png) ```python= class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: mem = {} mem = {len(text1) + 1, len(text2) + 1} for p1 = 0 : len(text1) - 1 for p2 0 : len(text2) - 1 l1 = text1[p1] l2 = text2[p2] # a a mem[1][1] = 1 + mem[0][0] if l1 == l2: mem[p1 + 1][p2 + 1] = 1 + mem[p1][p2] else: mem[p1 + 1][p2 + 1] = max(mem[p1+1, p2], mem[p1, p2+1]) return mem[len(text1)][len(text2)] def helper(p1, p2): # mem[i][j] != null l1 = text1[p1] l2 = text2[p2] if l1 == l2: return mem[i][j] = 1 + mem[p1 -1][p2-1] else: return mem[i][j] = max(helper(p1, p2 + 1), helper(p1 + 1, p2)) return helper(0, 0) # if p1 == len(text1) or p2 == len(text2): # return 0 # if (p1, p2) in mem: # return mem[(p1, p2)] # l1 = text1[p1] # occr = text2.find(l1, p2) # opt2 = 0 # opt1 = helper(p1+1, p2) # if occr != -1: # opt2 = 1+helper(p1+1, occr+1) # mem[(p1, p2)] = max(opt1, opt2) # return max(opt1, opt2) # return helper(0, 0) ```