-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh1092.py
36 lines (29 loc) · 1.12 KB
/
h1092.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
class Solution:
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
dp = [[i for i in range(len(str1) + 1)]] + \
[[i] + [None] * len(str1) for i in range(1, 1 + len(str2))]
dp[0][0] = 0
for i, ci in enumerate(str2, 1) :
for j, cj in enumerate(str1, 1) :
dp[i][j] = min(
dp[i - 1][j],
dp[i][j - 1],
dp[i - 1][j - 1] if ci == cj else inf # if same letter
) + 1
output = []
r, c = len(dp) - 1, len(dp[0]) - 1
while r > 0 and c > 0 :
if dp[r][c] == dp[r - 1][c - 1] + 1 and str1[c - 1] == str2[r - 1]:
c -= 1
r -= 1
output.append(str1[c])
continue
if dp[r][c - 1] == dp[r][c] - 1 :
c -= 1
output.append(str1[c])
continue
r -= 1
output.append(str2[r])
output.extend((reversed(str1[:c])))
output.extend((reversed(str2[:r])))
return ''.join(reversed(output))