1143. Longest Common Subsequence

202401291955
tags: #dp

from functools import lru_cache

class Solution:
	def longestCommonSubsequence(self, text1: str, text2: str) -> int:
		@lru_cache(maxsize=None)
		def solve(i1, i2):
			if i1 == len(text1) or i2 == len(text2):
				return 0
			if text1[i1] == text2[i2]:
				return 1 + solve(i1 + 1, i2 + 1)
			else:
				return max(solve(i1 + 1, i2), solve(i1, i2 + 1))
			return solve(0, 0)

Reference

https://leetcode.com/problems/longest-common-subsequence/