Shortest Common Supersequence - Problem

You're given two strings str1 and str2, and your task is to find the shortest possible string that contains both strings as subsequences.

A subsequence is formed by deleting some (possibly zero) characters from a string while maintaining the relative order of the remaining characters. For example, "ace" is a subsequence of "abcde".

The shortest common supersequence is the smallest string that has both input strings as subsequences. Think of it as merging two strings optimally while preserving their character order.

Example: For strings "abac" and "cab", one possible shortest common supersequence is "cabac" (length 5), which contains both "abac" and "cab" as subsequences.

Goal: Return any valid shortest common supersequence. If multiple solutions exist with the same minimum length, any of them is acceptable.

Input & Output

example_1.py — Basic Example
$ Input: {"str1": "abac", "str2": "cab"}
Output: "cabac"
💡 Note: Both 'abac' and 'cab' are subsequences of 'cabac'. The LCS is 'ca' with length 2, so minimum supersequence length is 4+3-2=5.
example_2.py — No Common Characters
$ Input: {"str1": "abc", "str2": "def"}
Output: "abcdef"
💡 Note: Since strings share no common characters, we simply concatenate them. LCS length is 0, so result length is 3+3-0=6.
example_3.py — One String is Subsequence
$ Input: {"str1": "abc", "str2": "aec"}
Output: "aebac"
💡 Note: The strings share common subsequence 'ac'. We merge optimally to get minimum length supersequence of length 3+3-2=4.

Constraints

  • 1 ≤ str1.length, str2.length ≤ 1000
  • str1 and str2 consist of lowercase English letters only
  • Important: Multiple valid answers may exist - return any shortest one

Visualization

Tap to expand
Shortest Common Supersequence INPUT str1 = "abac" a b a c str2 = "cab" c a b Goal: Find shortest string containing both as subsequences LCS("abac","cab") = "ab" Common chars in order: a b ALGORITHM STEPS 1 Find LCS Use DP to find LCS = "ab" 2 Build DP Table Track choices for backtrack c a b a 0 1 1 b 0 1 2 3 Backtrack Build SCS from end Match: add once No match: add from str1 or str2 Result: c + a + b + a + c 4 Reverse Result Get final SCS string SCS = m + n - LCS = 4 + 3 - 2 = 5 FINAL RESULT Shortest Common Supersequence: c a b a c Output: "cabac" Verification: str1 = "abac" in "cabac" c a b a c OK str2 = "cab" in "cabac" c a b a c OK Key Insight: The SCS length = len(str1) + len(str2) - len(LCS). By finding the LCS first using dynamic programming, we can backtrack through the DP table to construct the SCS by including each character exactly once when matched, and adding non-matching characters from both strings in the correct order. TutorialsPoint - Shortest Common Supersequence | Optimal Solution (DP with Backtracking)
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
67.4K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen