Solution: Shortest Common Supersequence (SCS)
Understand multiple dynamic programming techniques to solve the Shortest Common Supersequence problem. Learn brute force recursion, optimize with memoization, and apply tabularization to achieve efficient solutions with clear time and space complexity analyses.
Solution 1: Brute force
Explanation
In this solution, we try all the superstrings—one character at a time.
- If the sequences have a matching
i-th character, we skip one character from both the sequences and make a recursive call for the remaining lengths (lines 20–21). - If the characters don’t match, we call the function recursively twice by skipping one character from each string (lines 23–24).
- We return the minimum result of these two calls (line 26).
Time complexity
The time complexity of the above algorithm is exponential , where ...