-7 Removals
+10 Additions
from time import timefrom time import time
memo = {}
def LCS(X, Y):def LCS(X, Y):
if (X, Y) in memo:
return memo[(X, Y)]
if len(X) == 0 or len(Y) == 0: if len(X) == 0 or len(Y) == 0:
res = [] res = []
elif X[-1] == Y[-1]: elif X[-1] == Y[-1]:
res = LCS(X[:-1], Y[:-1]) + [X[-1]] res = LCS(X[:-1], Y[:-1]) + [X[-1]]
else: else:
res = longest(LCS(X, Y[:-1]), LCS(X[:-1], Y)) res = longest(LCS(X, Y[:-1]), LCS(X[:-1], Y))
memo[(X, Y)] = res
return res return res
def longest(X, Y):def longest(X, Y):
if len(X) > len(Y): return X if len(X) > len(Y) else Y
return X
else:
return Y
start = time()start = time()
print("".join(LCS("arn. schwarzenegger", "ar. chuarcheneger")))print("".join(LCS("arnold schwarzenegger", "anol chuarcheneger")))
print("Time elapsed: %.4fs" % (time()- start))print("Time elapsed: %.4fs" % (time() - start))
Editor
Original Text
Changed Text