2Brute2Best
🔥0

Shortest Common Supersequence

Hard·LeetCode #1092 Pro OnlyInterview-Level
LeetCode
01

Problem Statement

You are given two strings str1 and str2. Build the shortest string that contains both of them as subsequences: each of them must be obtainable from the answer by deleting some letters. Return one shortest string. If there are several, any of them is accepted.

Input

Two strings, str1 and str2.

Output

A shortest string that has both str1 and str2 as subsequences.

Example 1
Input: str1 = "abac", str2 = "cab"
Output: "cabac" (any shortest answer is accepted)

abac is in cabac if you delete the first c. cab is in cabac if you delete the last ac. Nothing shorter contains both, so the length is 5.

Example 2
Input: str1 = "aaaaaaaa", str2 = "aaaaaaaa"
Output: "aaaaaaaa"

The two strings are equal, so one copy is enough.

Constraints
  • • 1 ≤ str1.length, str2.length ≤ 1000
  • • Lowercase English letters only.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“shortest string that contains both as subsequences”
→ Letters that both strings share can be written once, so the length is n + m − LCS.
“return any shortest string”
→ One valid walk is enough, and the table tells us which walk is shortest.
“contains both strings”
→ The answer is a supersequence: letters are written, not dropped. That is the opposite direction of LCS.
03

Approach Complexity Ladder

Brute Force — try every way to write the next letterO(3^(N + M))O((N + M)²)

Recurse on (i, j): write str1's letter, write str2's letter, or share a match; keep the shortest

Optimal — LCS table, then walk back from the cornerO(N · M)O(N · M)

Build the LCS table, then write the answer while walking back from (n, m)

Brute Force Intuition

Let scs(i, j) be the shortest string that contains str1[i..] and str2[j..] as subsequences. There are three ways to write the next letter: write str1's letter alone, write str2's letter alone, or, when the letters match, write one letter for both. Try every option and keep the shortest. When one string is used up, the rest of the other string is the answer.

Recognition rule for this sub-pattern:

Match → 1 + dp[i−1][j−1]; otherwise max(dp[i−1][j], dp[i][j−1]). Counting versions add the cases instead of taking a max.
Remember this · DP — Strings
Trigger
Longest common subsequence, edit distance, palindromes, matching with wildcards, interleaving.
Think
Look at the last characters: if they match, what do I do? If not, what are my options?
Key question
What does dp[i][j] mean, and what are the empty-string base cases?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like DP — Strings translate into production architectures and low-level memory mechanics.