Solution: Shortest Common Supersequence
Explore different dynamic programming solutions for the shortest common supersequence problem. Understand brute force recursion, optimize with memoization, and implement a bottom-up tabular approach to develop efficient algorithms.
Solution #1: Brute force
First, let’s start by looking at the brute force solution to solve this problem.
Explanation
In this solution, we try all the superstrings one character at a time. The code does the following:
-
If the sequences have a matching ith character, the code skips one character from both the sequences and makes a recursive call for the remaining lengths (lines 11-12).
-
If the characters don’t match, the code calls the function recursively twice by skipping one character from each string (lines 14-15).
-
The code returns the minimum result of these two calls (line 17).
Time complexity
The time complexity of the above algorithm is exponential , where n and m are the lengths of the input ...