-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargest_common_subsequence.py
More file actions
138 lines (118 loc) · 4.17 KB
/
largest_common_subsequence.py
File metadata and controls
138 lines (118 loc) · 4.17 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def LCSubsequence_recursive(string_one, string_two, n, m, memo):
# Base Condition
if n == 0 or m == 0:
return 0
if memo[n][m] != -1:
return memo[n][m]
# Choice diagram
if string_one[n - 1] == string_two[m - 1]:
# PICK
memo[n][m] = 1 + LCSubsequence_recursive(
string_one, string_two, n - 1, m - 1, memo
)
else:
# NOT PICK
memo[n][m] = max(
LCSubsequence_recursive(string_one, string_two, n - 1, m, memo),
LCSubsequence_recursive(string_one, string_two, n, m - 1, memo),
)
return memo[n][m]
def LCSubsequence_tabulated(first, second):
n = len(first)
m = len(second)
memo = [[0] * (m + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
for j in range(1, m + 1):
if first[i - 1] == second[j - 1]:
memo[i][j] = 1 + memo[i - 1][j - 1]
else:
memo[i][j] = max(memo[i - 1][j], memo[i][j - 1])
return memo[n][m]
# -------------Problem Variations-----------------------
# 1. Print LCS -> Backtrack the sequence
def LCSubsequence(first, second):
n = len(first)
m = len(second)
memo = [[0] * (m + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
for j in range(1, m + 1):
if first[i - 1] == second[j - 1]:
memo[i][j] = 1 + memo[i - 1][j - 1]
else:
memo[i][j] = max(memo[i - 1][j], memo[i][j - 1])
ans = ""
while n > 0 and m > 0:
if first[n - 1] == second[m - 1]:
ans += first[n - 1]
n -= 1
m -= 1
elif memo[n - 1][m] > memo[n][m - 1]:
n -= 1
else:
m -= 1
return ans[::-1]
# 2. Largest Pallindromic Subsequence -> find lcs(str, reverse(str));
# 3. Subsequence Pattern Matching | Check whether given string "a" is subseqence of "b" or not
# -> len(lcs(a, b)) == len(a);
# 4. Min number of insertion and deletion to make string a == string b.
# -> deletion = str1.length - lcs.length, insertion = str2.length - lcs.length
# 5. Min number of deletion in a string to make it a pallindrome.
# -> str.length - lcs(str, reverse(str));
# 6. Min number of insertion in a string to make it a pallindrome.
# -> str.length - lcs(str, reverse(str));
# 7. Length Shortest Common SuperSequence
# -> str1.length + str2.length - lcs.length;
# 8. Print SCS
# -> While printing the lcs we only involve charactre present in both string here we also take characters when doesn't match.
# 9. Largest common Substring
def LCSubstring(first, second):
n = len(first)
m = len(second)
memo = [[0] * (m + 1) for _ in range(n + 1)]
max_len = 0
end_index = 0
for i in range(1, n + 1):
for j in range(1, m + 1):
if first[i - 1] == second[j - 1]:
memo[i][j] = 1 + memo[i - 1][j - 1]
if memo[i][j] > max_len:
max_len = memo[i][j]
end_index = i - 1
ans = first[end_index - max_len + 1 : end_index + 1]
return ans
# 10. Largest repeating subsequence
# lcs(str, str) where memo[i-1] == memo[j-1] && i != j
# 11. Length of largest subsequence of a which is a substring is b.
# 12. Count How many times a appear as subsequence in b.
# 13. Largest Pallindromic Substring
# lcs(str, str[::-1])
def longest_pallindromic_subtring(one):
n = len(one)
memo = [[0] * n for _ in range(n)]
start_ind = 0
max_len = 1
for r in range(n):
memo[r][r] = 1
for r in range(n - 1):
if one[r] == one[r + 1]:
memo[r][r + 1] = 1
start_ind = r
max_len = 2
for k in range(3, n + 1):
for r in range(n - k + 1):
c = r + k - 1
if one[r] == one[c] and memo[r + 1][c - 1] == 1:
memo[r][c] = 1
if k > max_len:
start_ind = r
max_len = k
ans = one[start_ind : start_ind + max_len]
return ans
# 14. Count Pallindromic Substring
if __name__ == "__main__":
one = "1235986"
two = "12345986"
# print(longest_pallindromic_subtring(one))
print("one:", one)
print("two:", two)
print(LCSubstring(one, two))