-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLongest Common Subsequence.py
113 lines (95 loc) · 3.88 KB
/
Longest Common Subsequence.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
Longest Common Subsequence o ☆
Write a function that takes in two strings and returns their longest common subsequence.
A subsequence of a string is a set of characters that aren't necessarily adjacent in the string but that are in
the same order as they appear in the string. For instance, the characters ["a", "c", "d"] form a
subsequence of the string "abcd", and so do the characters ["b", "d"] . Note that a single character
in a string and the string itself are both valid subsequences of the string.
You can assume that there will only be one longest common subsequence.
Sample Input
str1 = "ZXWYZW"
str2 = "XKYKZPW"
Sample Output
["x", "Y", "z", "W"]
"""
# SOLUTION 1
# O(nm*min(n, m)) time | 0(nm*min(n, m)) space
def longestCommonSubsequence(str1, str2):
lcs = [[[] for x in range(len(str1) + 1)] for y in range(len(str2) + 1)]
for i in range(1, len(str2) + 1):
for j in range(1, len(str1) + 1):
if str2[i - 1] == str1[j - 1]:
lcs[i][j] = lcs[i - 1][j - 1] + [str2[i - 1]]
else:
lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1], key=len)
return lcs[-1][-1]
# SOLUTION 2
# O(nm*min(n, m)) time | ((min(n, m))^2) space
def longestCommonSubsequence(str1, str2):
small = str1 if len(str1) < len(str2) else str2
big = str1 if len(str1) >= len(str2) else str2
evenLcs = [[] for x in range(len(small) + 1)]
oddLcs = [[] for x in range(len(small) + 1)]
for i in range(1, len(big) + 1):
if i % 2 == 1:
currentLcs = oddLcs
previousLcs = evenLcs
else:
currentLcs = evenLcs
previousLcs = oddLcs
for j in range(1, len(small) + 1):
if big[i - 1] == small[j - 1]:
currentLcs[j] = previousLcs[j - 1] + [big[i - 1]]
else:
currentLcs[j] = max(previousLcs[j], currentLcs[j - 1], key=len)
return evenLcs[-1] if len(big) % 2 == 0 else oddLcs[-1]
# SOLUTION 3
# O(nm) time | O(nm) space
def longestCommonSubsequence(str1, str2):
lcs = [[[None, 0, None, None] for x in range(len(str1) + 1)] for y in range(len(str2) + 1)]
for i in range(1, len(str2) + 1):
for j in range(1, len(str1) + 1):
if str2[i - 1] == str1[j - 1]:
lcs[i][j] = [str2[i - 1], lcs[i - 1][j - 1][1] + 1, i - 1, j - 1]
else:
if lcs[i - 1][j][1] > lcs[i][j - 1][1]:
lcs[i][j] = [None, lcs[i - 1][j][1], i - 1, j]
else:
lcs[i][j] = [None, lcs[i][j - 1][1], i, j - 1]
return buildsequence(lcs)
def buildsequence(lcs):
sequence = []
i = len(lcs) - 1
j = len(lcs[0]) - 1
while i != 0 and j != 0:
currentEntry = lcs[i][j]
if currentEntry[0] is not None:
sequence.append(currentEntry[0])
i = currentEntry[2]
j = currentEntry[3]
return list(reversed(sequence))
# SOLUTION 4
# O(nm) time | O(nm) space
def longestCommonSubsequence(str1, str2):
lengths = [[0 for x in range(len(str1) + 1)] for y in range(len(str2) + 1)]
for i in range(1, len(str2) + 1):
for j in range(1, len(str1) + 1):
if str2[i - 1] == str1[j - 1]:
lengths[i][j] = lengths[i - 1][j - 1] + 1
else:
lengths[i][j] = max(lengths[i - 1][j], lengths[i][j - 1])
return buildsequence(lengths, str1)
def buildsequence(lengths, string):
sequence = []
i = len(lengths) - 1
j = len(lengths[0]) - 1
while i != 0 and j != 0:
if lengths[i][j] == lengths[i - 1][j]:
i -= 1
elif lengths[i][j] == lengths[i][j - 1]:
j -= 1
else:
sequence.append(string[j - 1])
i -= 1
j -= 1
return list(reversed(sequence))