Last active
March 7, 2021 22:51
-
-
Save mvallebr/828f0d4727a2a50766ffd8c35af35c99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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