Skip to content

Instantly share code, notes, and snippets.

@vishesh
Created December 12, 2011 21:34
Show Gist options
  • Save vishesh/1469214 to your computer and use it in GitHub Desktop.
Save vishesh/1469214 to your computer and use it in GitHub Desktop.
Longest Common Subsequence
#!/usr/bin/python2
#
# Longest Common Subsequence problem
# Just implements simple lcs function. One solution, not all.
def lcs(x, y):
if len(x) == 0 or len(y) == 0:
return ""
elif x[-1:] == y[-1:]:
return lcs(x[:-1], y[:-1]) + x[-1:]
else:
a = lcs(x, y[:-1])
b = lcs(x[:-1], y)
if len(a) > len(b):
return a
else:
return b
# input
x = raw_input("Enter first string : ")
y = raw_input("Enter second string: ")
print "LCS = ", lcs(x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment