Shortest Common Supersequence
Last updated
Was this helpful?
Last updated
Was this helpful?
Given two sequences ‘s1’ and ‘s2’, write a method to find the length of the shortest sequence which has ‘s1’ and ‘s2’ as subsequences.
Example 1:
Example 2:
The problem is quite similar to the Longest Common Subsequence.
A basic brute-force solution could be to try all the super-sequences of the given sequences. We can process both of the sequences one character at a time, so at any step, we must choose between:
If the sequences have a matching character, we can skip one character from both the sequences and make a recursive call for the remaining lengths to get SCS.
If the strings don’t match, we start two new recursive calls by skipping one character separately from each string. The minimum of these two recursive calls will have our answer.
Directly jumping to bottom up since it's so similar to LCS