-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_common_subsequence.py
More file actions
61 lines (49 loc) · 2.1 KB
/
longest_common_subsequence.py
File metadata and controls
61 lines (49 loc) · 2.1 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
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
# Solution 1: 2D-Dynamic Programming with space optimisation
# Sort of 2D but every time we iterate the new row we write over the previous row data
# This makes our space O(min(M, N))
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
if len(text1) < len(text2):
text1, text2 = text2, text1
rows, cols = len(text1), len(text2)
dp = [0] * (cols + 1)
for row in range(1, rows + 1):
template_row = [0] * (cols + 1)
for column in range(1, cols + 1):
if text1[row - 1] == text2[column - 1]:
template_row[column] = dp[column - 1] + 1
else:
template_row[column] = max(template_row[column - 1], dp[column])
dp = template_row
return dp[cols]
# Solution 2: 2D-Dynamic Programming
# Time and space: O(M * N)
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
rows, cols = len(text1), len(text2)
dp = [[0] * (cols + 1) for _ in range(rows + 1)]
for row in range(1, rows + 1):
for column in range(1, cols + 1):
if text1[row - 1] == text2[column - 1]:
dp[row][column] = dp[row - 1][column - 1] + 1
else:
dp[row][column] = max(dp[row][column - 1], dp[row - 1][column])
return dp[rows][cols]
# Solution 3: Memoization
# Time and space: O(M * N)
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
memo = {}
def check(ptr1, ptr2):
if ptr1 < 0 or ptr2 < 0:
return 0
if (ptr1, ptr2) in memo:
return memo[(ptr1, ptr2)]
if text1[ptr1] == text2[ptr2]:
memo[(ptr1, ptr2)] = check(ptr1 - 1, ptr2 - 1) + 1
else:
memo[(ptr1, ptr2)] = max(check(ptr1 - 1, ptr2), check(ptr1, ptr2 - 1))
return memo[(ptr1, ptr2)]
return check(len(text1) - 1, len(text2) - 1)
# test = Solution()
# print(test.longestCommonSubsequence("abcde", "ace"))