Problem description
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If multiple answers exist, you may return any of them.
(A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)
Example 1:
Input: str1 = “abac”, str2 = “cab” Output: “cabac” Explanation: str1 = “abac” is a substring of “cabac” because we can delete the first “c”. str2 = “cab” is a substring of “cabac” because we can delete the last “ac”. The answer provided is the shortest such string that satisfies these properties.
Note:
1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
Solution
Considering calculating the length of the longest common subsequences. Let DP[i][j] be the length of the longest common subsequences of str1[:i] and str2[:j].
- If str1[i - 1] == str2[j - 1], then DP[i][j] = 1 + DP[i - 1][j - 1]
- If str1[i - 1] != str2[j - 1], then we can either choose to keep str1[i - 1] or str2[j - 1]. So there are two options for DP[i][j]. That is 1 + DP[i - 1][j] and 1 + DP[i][j - 1], we take the maximum of these two.
After we obtained the answer for DP[i][j], we can build the longest common subsequence backward by examining the option we took everytime.
Below is my python implementation
class Solution:
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
m = len(str1)
n = len(str2)
DP = [[0 for j in range(n + 1)] for i in range(m + 1)]
res = []
for i in range(m + 1):
DP[i][0] = i
for j in range(n + 1):
DP[0][j] = j
for i in range(1, m + 1):
for j in range(1, n + 1):
if str1[i - 1] == str2[j - 1]:
DP[i][j] = 1 + DP[i - 1][j - 1]
else:
if DP[i - 1][j] < DP[i][j - 1]:
DP[i][j] = 1 + DP[i - 1][j]
else:
DP[i][j] = 1 + DP[i][j - 1]
#print(DP[m][n])
#build string from DP array
i = m
j = n
res = []
while i > 0 or j > 0:
if i - 1 >= 0 and j - 1 >= 0 and str1[i - 1] == str2[j - 1]:
res.append(str1[i - 1])
i -= 1
j -= 1
else:
if DP[i - 1][j] < DP[i][j - 1]:
res.append(str1[i - 1])
i -= 1
else:
res.append(str2[j - 1])
j -= 1
return ''.join(reversed(res))