Skip to content

Commit 1592f48

Browse files
committed
[main] Added longest substring problem
1 parent aae7735 commit 1592f48

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

pythonProblems/dp/lcs.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,68 @@ def lcs(substr1: str, substr2: str) -> int:
1515
return 1 + lcs(substr1[:-1], substr2[:-1])
1616
return 0
1717

18+
def print_lcs(substr1: str, substr2: str) -> str:
19+
table = []
20+
# initialize table
21+
for _ in substr1 + " ":
22+
table_row = []
23+
for __ in substr2 + " ":
24+
table_row.append(0)
25+
table.append(table_row)
26+
table_row = []
27+
28+
for i in range(1, len(substr1) + 1):
29+
for j in range(1, len(substr2) + 1):
30+
if substr2[j - 1] == substr1[i - 1]: ## if both characters match
31+
table[i][j] = table[i - 1][j - 1] + 1 ## characters match, set the diagonal cell to the curr value + 1
32+
else:
33+
table[i][j] = max(table[i - 1][j], table[i][j - 1]) ## either up or left in the lookup table
34+
## start at bottom right, and follow the path, taking a diagonal left with the letters are the same
35+
36+
startCol = len(table[len(table) - 1]) - 1
37+
startRow = len(table) - 1
38+
39+
_substr1 = " " + substr1
40+
_substr2 = " " + substr2
41+
substr = ''
42+
pprint(table)
43+
while startCol != 0 and startRow != 0:
44+
if _substr1[startRow] == _substr2[startCol]:
45+
## jump diagonal, add letter
46+
substr += _substr1[startRow]
47+
startRow -= 1
48+
startCol -= 1
49+
else:
50+
## evaluate left
51+
left_ = -1
52+
if startCol > 0:
53+
left_ = table[startRow][startCol - 1]
54+
## evaluate top
55+
top_ = -1
56+
if startRow > 0:
57+
top_ = table[startRow - 1][startCol]
58+
59+
if left_ == top_:
60+
startCol -= 1
61+
if left_ != -1 and top_ != -1:
62+
startRow -= 1 if top_ > left_ else 0
63+
startCol -= 1 if left_ > top_ else 0
64+
if left_ == -1:
65+
## move up
66+
startRow -= 1
67+
if top_ == -1:
68+
startCol -= 1
69+
return substr[::-1]
70+
71+
1872

1973
if __name__ == '__main__':
2074
x = 'abcbdab'
2175
y = 'bdcaba'
2276
print(lcs(x, y))
77+
x2 = 'xmjyauzadfsf'
78+
y2 = 'mzjawxuwqrwe'
79+
print(print_lcs(x2, y2))
2380

2481

2582

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from pprint import pprint
2+
3+
def longestSubstring(substr1: str, substr2: str) -> str:
4+
table = []
5+
for _ in substr1:
6+
table_row = []
7+
for __ in substr2:
8+
table_row.append(0)
9+
table.append(table_row)
10+
table_row = []
11+
12+
for i in range(len(substr1)):
13+
for j in range(len(substr2)):
14+
if substr1[i] == substr2[j]:
15+
if i == 0:
16+
table[i][j] = 1
17+
elif j == 0:
18+
table[i][j] = 1
19+
else:
20+
table[i][j] = table[i - 1][j - 1] + 1
21+
22+
coords = []
23+
max_value = 0
24+
for eachrow in table:
25+
max_value = max(max_value, max(eachrow))
26+
27+
for i in range(len(table)):
28+
for j in range(len(table[i])):
29+
if table[i][j] == max_value:
30+
coords.append((i, j))
31+
32+
substrs = []
33+
for eachcoord in coords:
34+
x_ = eachcoord[1]
35+
y_ = eachcoord[0]
36+
substr = ''
37+
while table[y_][x_] != 0 and x_ >= 0 and y_ >= 0:
38+
substr += substr1[x_]
39+
x_ -= 1
40+
y_ -= 1
41+
substrs.append(substr)
42+
print(substrs)
43+
44+
45+
46+
47+
if __name__ == '__main__':
48+
s1 = 'abab'
49+
s2 = 'baba'
50+
longestSubstring(s1, s2)

0 commit comments

Comments
 (0)