Skip to content

Instantly share code, notes, and snippets.

@mvallebr
Last active March 7, 2021 22:51
Show Gist options
  • Select an option

  • Save mvallebr/828f0d4727a2a50766ffd8c35af35c99 to your computer and use it in GitHub Desktop.

Select an option

Save mvallebr/828f0d4727a2a50766ffd8c35af35c99 to your computer and use it in GitHub Desktop.
from functools import lru_cache
class Solution:
def longestCommonSubsequence(self, word1: str, word2: str) -> int:
@lru_cache(None)
def dfs(i, j):
if i == len(word1) or j == len(word2):
return 0
if word1[i] == word2[j]:
return dfs(i + 1, j + 1) + 1
else:
return max(dfs(i, j + 1), dfs(i + 1, j))
return dfs(0, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment