1092. Shortest Common Supersequence
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 subsequence of "cabac" because we can delete the first "c".
str2 = "cab" is a subsequence 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 <= 1000str1andstr2consist of lowercase English letters.
// Dynamic Programming + LCS Calculation
string shortestCommonSupersequence(string str1, string str2) { // time: O(m * n); space: O(m * n)
int m = str1.length(), n = str2.length();
vector<vector<int> > dp(m + 1, vector<int> (n + 1, 0));
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (str1[i - 1] == str2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
string res;
int i = m - 1, j = n - 1;
while (i >= 0 || j >= 0) {
if ((i < 0) ^ (j < 0)) {
res += (i < 0) ? str2[j--] : str1[i--];
} else if(str1[i] == str2[j]) {
res += str1[i];
i--, j--;
} else {
res += dp[i + 1][j] > dp[i][j + 1] ? str2[j--] : str1[i--];
}
}
reverse(res.begin(), res.end());
return res;
}Last updated
Was this helpful?