forked from rajnishmaurya73/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS
More file actions
38 lines (34 loc) · 978 Bytes
/
Copy pathLCS
File metadata and controls
38 lines (34 loc) · 978 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
# Longest common subsequence
{
def longest_common_subsequence(x, y):
"""Longest common subsequence
Dynamic programming
:param x:
:param y: x,y are lists or strings
:returns: longest common subsequence in form of a string
:complexity: `O(|x|*|y|)`
"""
n = len(x)
m = len(y)
# -- calcul longueur optimale
A = [[0 for j in range(m + 1)] for i in range(n + 1)]
for i in range(n):
for j in range(m):
if x[i] == y[j]:
A[i + 1][j + 1] = A[i][j] + 1
else:
A[i + 1][j + 1] = max(A[i][j + 1], A[i + 1][j])
# -- extraire solution
sol = []
i, j = n, m
while A[i][j] > 0:
if A[i][j] == A[i - 1][j]:
i -= 1
elif A[i][j] == A[i][j - 1]:
j -= 1
else:
i -= 1
j -= 1
sol.append(x[i])
return ''.join(sol[::-1]) # liste inversée
}