Created
December 12, 2011 21:34
-
-
Save vishesh/1469214 to your computer and use it in GitHub Desktop.
Longest Common Subsequence
This file contains 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
#!/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