Skip to content

Instantly share code, notes, and snippets.

@arrieta
Created December 6, 2017 15:33
Show Gist options
  • Save arrieta/ddbc043f068fe402ad3f47660c0edcd9 to your computer and use it in GitHub Desktop.
Save arrieta/ddbc043f068fe402ad3f47660c0edcd9 to your computer and use it in GitHub Desktop.
Longest Common Subsequence
def lcs(s1, s2):
def lcs_(s1, s2, m, n):
if (m < 0) or (n < 0):
return 0
elif s1[m] == s2[n]:
return 1 + lcs_(s1, s2, m - 1, n - 1)
else:
return max(lcs_(s1, s2, m - 1, n),
lcs_(s1, s2, m, n - 1))
return lcs_(s1, s2, len(s1) - 1, len(s2) - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment