Search⌘ K
AI Features

Solution: Shortest Common Supersequence

Explore how to solve the shortest common supersequence problem by applying dynamic programming concepts. Understand how to use the longest common subsequence as a foundation to build the shortest sequence that includes both input strings as subsequences. This lesson helps you implement an efficient DP solution with step-by-step instructions and analysis of time and space complexity.

Statement

You are given two strings, str1 and str2. Your task is to find the shortest common supersequence (SCS). The shortest possible string that contains both str1 and str2 as subsequences.

If multiple strings satisfy this condition, you may return any one of them.

Note: A string ss is considered a subsequence of another string tt if ss can be obtained by deleting zero or more characters from tt without changing the order of the remaining characters.

Constraints:

  • 11 \leq str1.length, str2.length 103\leq 10^3 ...