-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCS.py
38 lines (33 loc) · 960 Bytes
/
LCS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# --*-- coding: utf-8 --*--
def lcs(s1, s2):
"""
LCS Program for 2 strings
"""
cache = [[None] * len(s2)] * len(s1)
return calculateLCS(s1, s2, 0, 0, cache)
def calculateLCS(s1, s2, i1, i2, cache):
"""
logic to calculate LCS
"""
if len(s1) > i1 and len(s2) > i2:
if cache[i1][i2]:
return cache[i1][i2]
elif s1[i1] == s2[i2]:
cache[i1][i2] = s1[i1] + calculateLCS(s1, s2, i1 + 1, i2 + 1, cache)
return cache[i1][i2]
else:
cache[i1][i2] = max_len(calculateLCS(s1, s2, i1 + 1, i2, cache), calculateLCS(s1, s2, i1, i2 + 1, cache))
return cache[i1][i2]
else:
return ''
def max_len(s1, s2):
"""
Find string with max length
"""
if len(s1) >= len(s2):
return s1
else:
return s2
s1 = input("Enter String 1: ")
s2 = input("Enter String 2: ")
print(lcs(s1,s2))