Problem Description
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. A string s is a subsequence of string t if deleting some (possibly zero) characters from t results in s. If there exist multiple valid answers, any one of them is acceptable.
Key Insights
- The problem can be solved by leveraging the concept of the Longest Common Subsequence (LCS).
- By first finding the LCS of str1 and str2, we can merge the two strings while ensuring that common parts (LCS) are not repeated unnecessarily.
- Dynamic Programming is used to compute the LCS.
- Once the LCS is available, the final result is built by interleaving characters from str1 and str2 while aligning with the LCS.
Space and Time Complexity
Time Complexity: O(m * n), where m and n are the lengths of str1 and str2 respectively. Space Complexity: O(m * n) for the DP table to store LCS lengths, with additional space for the output string.
Solution
The solution is divided into two main steps:
- Compute the LCS of the two strings using a Dynamic Programming table. This table dp[i][j] represents the length of the LCS of str1[0...i-1] and str2[0...j-1].
- Reconstruct the shortest common supersequence by traversing the two strings in parallel and using the LCS as a guide. For every character in the LCS, append all non-LCS characters from str1 and str2 until that common character is reached, then add the LCS character. Append any remaining characters after processing the LCS.
The key observation is that by including the non-matching parts between matched LCS characters, we ensure both strings are subsequences of the constructed result, and the result is the shortest possible.