11. Shortest Common Supersequence
โ GFG solution to the Shortest Common Supersequence problem: find the length of smallest string containing both input strings as subsequences using dynamic programming. ๐
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
Given two strings s1 and s2, find the length of the smallest string which has both s1 and s2 as its subsequences.
A supersequence is a sequence that contains both strings as subsequences. The goal is to find the minimum length of such a supersequence where characters from both strings can be found in order (not necessarily contiguous).
Note: s1 and s2 can have both uppercase and lowercase English letters.
๐ Examples
Example 1
Input: s1 = "geek", s2 = "eke"
Output: 5
Explanation: String "geeke" has both string "geek" and "eke" as subsequences.Example 2
Input: s1 = "AGGTAB", s2 = "GXTXAYB"
Output: 9
Explanation: String "AGXGTXAYB" has both string "AGGTAB" and "GXTXAYB" as subsequences.Example 3
๐ Constraints
$1 \le \text{s1.size(), s2.size()} \le 500$
โ
My Approach
The optimal approach uses Space-Optimized Dynamic Programming to build the shortest common supersequence length:
Space-Optimized DP
Define DP State:
dp[i][j]represents the length of shortest common supersequence for firsticharacters ofs1and firstjcharacters ofs2.To optimize space, use only two 1D arrays:
prevandcurr.
Base Cases:
If
s1is empty (i = 0), we need all characters froms2, so length =j.If
s2is empty (j = 0), we need all characters froms1, so length =i.
State Transitions:
If characters match (
s1[i-1] == s2[j-1]): Include the character once.curr[j] = 1 + prev[j-1]
If characters don't match: Take minimum of two choices and add 1.
curr[j] = 1 + min(prev[j], curr[j-1])prev[j]means skip current character froms1curr[j-1]means skip current character froms2
Iterate Through Strings:
Process each character of both strings using nested loops.
Update
currarray based onprevarray values.After processing each row, copy
currtoprevfor next iteration.
Return Result:
Final answer is stored in
prev[n]after processing all characters.
๐ Time and Auxiliary Space Complexity
Expected Time Complexity: O(m ร n), where m is the length of string s1 and n is the length of string s2. We iterate through all possible combinations of characters from both strings using nested loops, performing constant-time operations for each cell.
Expected Auxiliary Space Complexity: O(n), where n is the length of string s2. We use two 1D arrays of size (n+1) to store previous and current row values, eliminating the need for a full 2D DP table and achieving space optimization.
๐งโ๐ป Code (C)
๐งโ๐ป Code (C++)
โก View Alternative Approaches with Code and Analysis
๐ 2๏ธโฃ LCS-Based Formula Approach
๐ก Algorithm Steps:
Calculate the length of Longest Common Subsequence (LCS) between two strings.
Use the formula: SCS length = m + n - LCS length.
This works because common characters need to be included only once.
Build LCS using standard dynamic programming approach.
๐ Complexity Analysis:
Time: โฑ๏ธ O(m ร n) - Nested iteration through both strings
Auxiliary Space: ๐พ O(n) - Two 1D arrays for DP
โ
Why This Approach?
Mathematical elegance using LCS formula
Easy to understand relationship between LCS and SCS
Reusable LCS logic for similar problems
๐ 3๏ธโฃ 2D DP Table Approach
๐ก Algorithm Steps:
Create a 2D DP table where dp[i][j] represents SCS length for first i characters of s1 and first j characters of s2.
Initialize base cases: dp[0][j] = j and dp[i][0] = i.
For each cell, if characters match, add 1 to diagonal value.
If characters don't match, take minimum of left and top cells and add 1.
๐ Complexity Analysis:
Time: โฑ๏ธ O(m ร n) - Double nested loop
Auxiliary Space: ๐พ O(m ร n) - 2D DP table
โ
Why This Approach?
Clear visualization of state transitions
Easy to trace back for constructing actual supersequence
Standard DP pattern for string problems
๐ 4๏ธโฃ Recursive with Memoization
๐ก Algorithm Steps:
Define recursive function that explores character matching decisions.
Base cases: if either string is empty, return length of other string.
If characters match, include once and recurse on remaining strings.
If not match, try both options and take minimum plus 1.
๐ Complexity Analysis:
Time: โฑ๏ธ O(m ร n) - Each state computed once with memoization
Auxiliary Space: ๐พ O(m ร n) - Memoization table and recursion stack
โ
Why This Approach?
Top-down approach easier to conceptualize
Natural representation of decision tree
Good for understanding problem structure
๐ ๐ Comparison of Approaches
๐ Approach
โฑ๏ธ Time Complexity
๐พ Space Complexity
โ Pros
โ ๏ธ Cons
๐ท๏ธ Space Optimized DP
๐ข O(m ร n)
๐ข O(n)
๐ Optimal space usage
๐ง Cannot backtrack for actual string
๐ LCS-Based Formula
๐ข O(m ร n)
๐ข O(n)
๐ Mathematical elegance
๐งฎ Two-step computation
๐ 2D DP Table
๐ข O(m ร n)
๐ก O(m ร n)
๐ฏ Easy to visualize
๐พ Higher space usage
๐ Recursive Memoization
๐ข O(m ร n)
๐ก O(m ร n)
โญ Intuitive logic
๐ Recursion overhead
๐ Best Choice Recommendation
๐ฏ Scenario
๐๏ธ Recommended Approach
๐ฅ Performance Rating
๐ Optimal performance needed
๐ฅ Space Optimized DP
โ โ โ โ โ
๐ Understanding problem structure
๐ฅ LCS-Based Formula
โ โ โ โ โ
๐ง Need to construct actual string
๐ฅ 2D DP Table
โ โ โ โ โ
๐ฏ Interview/Learning
๐ Recursive Memoization
โ โ โ โ โ
โ Code (Java)
๐ Code (Python)
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Let's make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated