-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_common_subsequence.py
More file actions
36 lines (32 loc) · 1.01 KB
/
Copy pathlongest_common_subsequence.py
File metadata and controls
36 lines (32 loc) · 1.01 KB
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
class Solution:
def longestCommonSubsequence(self, first: str, second: str) -> str:
n = len(first)
m = len(second)
mat = [[0 for i in range(m+1)] for j in range(n+1)]
for i in range(n):
for j in range(m):
if first[i] == second[j]:
mat[i+1][j+1] = mat[i][j] + 1
else:
mat[i+1][j+1] = max(mat[i+1][j], mat[i][j+1])
# code to create LCS
index = mat[n][m]
lcs = ['']*(index+1)
i = n
j = m
while i > 0 and j > 0:
if first[i-1] == second[j-1]:
lcs[index-1] = first[i-1]
i = i - 1
j = j - 1
index = index - 1
elif mat[i-1][j] > mat[i][j-1]:
i = i - 1
else:
j = j-1
return ''.join(lcs)
if __name__ == '__main__':
first = 'AGGTAB'
second = 'GXTXAYB'
sol = Solution()
print(sol.longestCommonSubsequence(first, second))