Advertisement

Shortest Common Supersequence - LeetCode 1092 Solution

Shortest Common Supersequence - Complete Solution Guide

Shortest Common Supersequence is LeetCode problem 1092, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given two strings str1 and str2 , return the shortest string that has both str1 and str2 as subsequences . If there are multiple valid strings, return any of them. A string s is a subsequence of string t if deleting some number of characters from t (possibly 0 ) 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 d

Detailed Explanation

The problem asks us to find the shortest string that contains both input strings, `str1` and `str2`, as subsequences. A subsequence is a sequence of characters that can be derived from another sequence by deleting some or no characters without changing the order of the remaining characters. The goal is to find the shortest possible string that fulfills this condition, and if multiple such strings exist, returning any one of them is acceptable. The inputs are two strings, `str1` and `str2`, consisting of lowercase English letters, with lengths between 1 and 1000.

Solution Approach

The solution uses dynamic programming to find the longest common subsequence (LCS) of the two input strings. The LCS is then used to construct the shortest common supersequence. The DP table `dp[i][j]` stores the length of the LCS of `str1[0...i-1]` and `str2[0...j-1]`. After building the DP table, we trace back from `dp[m][n]` to construct the supersequence. If `str1[i-1]` and `str2[j-1]` are equal, it means they are part of the LCS, so we add that character to our result and move diagonally up and to the left. If they are not equal, we compare `dp[i-1][j]` and `dp[i][j-1]` to see which way we came from. If `dp[i-1][j]` is greater, we add `str1[i-1]` to the result and move up. Otherwise, we add `str2[j-1]` to the result and move to the left. Once we have traced all the way back to either the top or the left edge of the DP table, we add any remaining characters from the other string to the result. Finally, we reverse the result to get the shortest common supersequence.

Step-by-Step Algorithm

  1. Step 1: Initialize a 2D DP table `dp` of size `(m+1) x (n+1)` with all values set to 0, where `m` is the length of `str1` and `n` is the length of `str2`.
  2. Step 2: Iterate through the DP table from `i = 1` to `m` and `j = 1` to `n`.
  3. Step 3: If `str1[i-1]` is equal to `str2[j-1]`, then `dp[i][j] = 1 + dp[i-1][j-1]`. This means we've found a common character and we extend the LCS by 1.
  4. Step 4: If `str1[i-1]` is not equal to `str2[j-1]`, then `dp[i][j] = max(dp[i-1][j], dp[i][j-1])`. This means we take the maximum LCS length by either skipping a character from `str1` or skipping a character from `str2`.
  5. Step 5: After filling the DP table, initialize an empty string `res` to store the shortest common supersequence.
  6. Step 6: Initialize `i = m` and `j = n` to start from the bottom-right corner of the DP table.
  7. Step 7: While `i > 0` and `j > 0`:
  8. - If `str1[i-1]` is equal to `str2[j-1]`, append the character `str1[i-1]` to `res`, decrement `i` and `j`.
  9. - Else if `dp[i-1][j] > dp[i][j-1]`, append the character `str1[i-1]` to `res`, decrement `i`.
  10. - Else, append the character `str2[j-1]` to `res`, decrement `j`.
  11. Step 8: While `i > 0`, append the character `str1[i-1]` to `res`, decrement `i`.
  12. Step 9: While `j > 0`, append the character `str2[j-1]` to `res`, decrement `j`.
  13. Step 10: Reverse the string `res` to obtain the shortest common supersequence.
  14. Step 11: Return the string `res`.

Key Insights

  • Insight 1: The length of the shortest common supersequence is the sum of the lengths of the two strings minus the length of their longest common subsequence (LCS).
  • Insight 2: Dynamic programming can be used to efficiently find the length of the longest common subsequence and also to reconstruct the shortest common supersequence.
  • Insight 3: When reconstructing the supersequence, if characters at the current indices of `str1` and `str2` are equal, that character is part of the LCS and included in the supersequence once. Otherwise, we include the character from whichever string contributed to the longer LCS length at that point (or either if the LCS lengths are equal).

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(m*n)

Topics

This problem involves: String, Dynamic Programming.

Companies

Asked at: Dream11.