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++)
โ 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