forked from aman-raza/Friends_Hack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestCommonSubsequence.py
More file actions
43 lines (33 loc) · 952 Bytes
/
longestCommonSubsequence.py
File metadata and controls
43 lines (33 loc) · 952 Bytes
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
39
40
41
42
43
def lcs(u, v):
c = [[-1]*(len(v) + 1) for _ in range(len(u) + 1)]
lcs_helper(u, v, c, 0, 0)
return c
def lcs_helper(u, v, c, i, j):
if c[i][j] >= 0:
return c[i][j]
if i == len(u) or j == len(v):
q = 0
else:
if u[i] == v[j]:
q = 1 + lcs_helper(u, v, c, i + 1, j + 1)
else:
q = max(lcs_helper(u, v, c, i + 1, j),
lcs_helper(u, v, c, i, j + 1))
c[i][j] = q
return q
def print_lcs(u, v, c):
i = j = 0
while not (i == len(u) or j == len(v)):
if u[i] == v[j]:
print(u[i], end='')
i += 1
j += 1
elif c[i][j + 1] > c[i + 1][j]:
j += 1
else:
i += 1
u = input('Enter first string: ')
v = input('Enter second string: ')
c = lcs(u, v)
print('Longest Common Subsequence: ', end='')
print_lcs(u, v, c)