Solution: Shortest Common Supersequence
This review provides a detailed analysis of the different ways to solve the shortest common supersequence problem.
We'll cover the following...
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 ...