Longest Common Subsequence LeetCode solution in Python
1class Solution:2def longestCommonSubsequence(self, text1: str, text2: str) -> int:3def lcs(ind1, ind2):4prev = [0 for i in range(ind2 + 1)]5curr = [0 for i in range(ind2 + 1)]67for i in range(1, ind1 + 1):8for j in range(1, ind2 + 1):9if text1[i - 1] == text2[j - 1]:10curr[j] = 1 + prev[j - 1]1112else:13curr[j] = max(prev[j], curr[j - 1])14prev = list(curr) # remember to use a new list for prev1516return prev[-1]1718ans = lcs(len(text1), len(text2))19return ans