-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path114.py
More file actions
35 lines (29 loc) · 1.55 KB
/
114.py
File metadata and controls
35 lines (29 loc) · 1.55 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
class Solution:
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
str1_length = len(str1)
str2_length = len(str2)
# Initialize the first row (when str1 is empty, the supersequence is str2's prefix)
prev_row = [str2[0:col] for col in range(str2_length + 1)]
# Fill the DP table row by row
for row in range(1, str1_length + 1):
# Initialize the first column (when str2 is empty, the supersequence is str1's prefix)
curr_row = [str1[0:row]] + [None for _ in range(str2_length)]
for col in range(1, str2_length + 1):
# If characters match, extend the supersequence from the diagonal value
if str1[row - 1] == str2[col - 1]:
curr_row[col] = prev_row[col - 1] + str1[row - 1]
else:
# If characters do not match, choose the shorter supersequence
# From previous row (exclude current str1 char)
pick_s1 = prev_row[col]
# From previous column (exclude current str2 char)
pick_s2 = curr_row[col - 1]
curr_row[col] = (
pick_s1 + str1[row - 1]
if len(pick_s1) < len(pick_s2)
else pick_s2 + str2[col - 1]
)
# Move to the next row (update previous row reference)
prev_row = curr_row
# Return the shortest common supersequence from the last cell
return prev_row[str2_length]