Skip to content

Instantly share code, notes, and snippets.

@Desolve
Created June 5, 2020 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Desolve/69e75a7085dbf609d186a1edf5967951 to your computer and use it in GitHub Desktop.
Save Desolve/69e75a7085dbf609d186a1edf5967951 to your computer and use it in GitHub Desktop.
1143 Longest Common Subsequence
class Solution:
def longestCommonSubsequence(self, t1: str, t2: str) -> int:
l1, l2 = len(t1), len(t2)
dp = [[0]*(l2+1) for _ in range(l1+1)]
for i in range(1, l1+1):
for j in range(1, l2+1):
if t1[i-1] == t2[j-1]:
dp[i][j] = dp[i-1][j-1] +1
else:
dp[i][j] = max(dp[i][j-1], dp[i-1][j])
return dp[l1][l2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment