0% completed
Problem Statement
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 2:
Input: s1: "abcf" s2:"bdcf"
Output: 5
Explanation: The shortest common super-sequence (SCS) is "abdcf".
Example 2:
Input: s1: "dynamic" s2:"programming"
Output: 15
Explanation: The SCS is "dynprogrammicng".
Constraints:
1 <= s1.length, s2.length <= 1000
s1
ands2
consist of lowercase English letters.
Basic Solution
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.
Here is the code:
The time complexity of the above algorithm is exponential O(2^{n+m}), where 'n' and 'm' are the lengths of the input sequences. The space complexity is O(n+m) which is used to store the recursion stack.
Top-down Dynamic Programming with Memoization
Let's use memoization to overcome the overlapping subproblems.
The two changing values to our recursive function are the two indexes, i1 and i2. Therefore, we can store the results of all the subproblems in a two-dimensional array. (Another alternative could be to use a hash-table whose key would be a string (i1 + “|” + i2)).
Code
Here is the code:
Bottom-up Dynamic Programming
Since we want to match all the subsequences of the given sequences, we can use a two-dimensional array to store our results. The lengths of the two strings will define the size of the array’s dimensions. So for every index ‘i’ in sequence ‘s1’ and ‘j’ in sequence ‘s2’, we will choose one of the following two options:
- If the character
s1[i]
matchess2[j]
, the length of the SCS would be the one plus the length of the SCS tilli-1
andj-1
indexes in the two strings. - If the character
s1[i]
does not matchs2[j]
, we will consider two SCS: one withouts1[i]
and one withouts2[j]
. Our required SCS length will be the shortest of these two super-sequences plus one.
So our recursive formula would be:
if s1[i] == s2[j]
dp[i][j] = 1 + dp[i-1][j-1]
else
dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1])
Let’s draw this visually for “abcf” and “bdcf”. Starting with a subsequence of zero length. As we can see, if any strings have zero length, then the shortest super-sequence would be equal to the length of the other string:

![i=1, j=1 => since s1[i-1] != s2[j-1], therefore dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1])](/http/77726476706e69737468656265737421e7e056d223357b5979068eb98a40307baa33/_next/image?vpn-1&url=http%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f64d0046a2a95a84d1556%252Fimg%3A3b32ede-3ab-6472-abdc-aa4c30d6214.jpg%3Fgeneration%3D1669293570297260%26alt%3Dmedia&w=3840&q=75&dpl=dpl_55x6JA8AzfKRV7dmqigEKECnnSPQ)
![i=1, j=3-4 => since s1[i] != s2[j], therefore dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1])](/http/77726476706e69737468656265737421e7e056d223357b5979068eb98a40307baa33/_next/image?vpn-1&url=http%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f64d0046a2a95a84d1556%252Fimg%3A31037c-45d8-52f4-f2fc-2ea3ca530a7.jpg%3Fgeneration%3D1669293621480723%26alt%3Dmedia&w=3840&q=75&dpl=dpl_55x6JA8AzfKRV7dmqigEKECnnSPQ)
![i=2, j=3-4 => since s1[i] != s2[j], therefore dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1])](/http/77726476706e69737468656265737421e7e056d223357b5979068eb98a40307baa33/_next/image?vpn-1&url=http%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f64d0046a2a95a84d1556%252Fimg%3A1364330-14b2-bffd-1135-8f134fbd1042.jpg%3Fgeneration%3D1669293695837429%26alt%3Dmedia&w=3840&q=75&dpl=dpl_55x6JA8AzfKRV7dmqigEKECnnSPQ)
![i=3, j=4 => since s1[i-1] == s2[j-1], therefore dp[i][j] = 1 + dp[i-1][j-1]](/http/77726476706e69737468656265737421e7e056d223357b5979068eb98a40307baa33/_next/image?vpn-1&url=http%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f64d0046a2a95a84d1556%252Fimg%3A7a13ee3-3dc3-488-d15a-7746882bc1.jpg%3Fgeneration%3D1669293722248284%26alt%3Dmedia&w=3840&q=75&dpl=dpl_55x6JA8AzfKRV7dmqigEKECnnSPQ)
![i=4, j=1-3 => since s1[i] != s2[j], therefore dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1])](/http/77726476706e69737468656265737421e7e056d223357b5979068eb98a40307baa33/_next/image?vpn-1&url=http%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f64d0046a2a95a84d1556%252Fimg%3A2a603a2-1846-86dc-241-b1de32bab546.jpg%3Fgeneration%3D1669293762717338%26alt%3Dmedia&w=3840&q=75&dpl=dpl_55x6JA8AzfKRV7dmqigEKECnnSPQ)
![i=4, j=4 => since s1[i-1] == s2[j-1], therefore dp[i][j] = 1 + dp[i-1][j-1]](/http/77726476706e69737468656265737421e7e056d223357b5979068eb98a40307baa33/_next/image?vpn-1&url=http%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F637f64d0046a2a95a84d1556%252Fimg%3Aea3e67-c17-8a06-aaa1-10511cf4decf.jpg%3Fgeneration%3D1669293777325508%26alt%3Dmedia&w=3840&q=75&dpl=dpl_55x6JA8AzfKRV7dmqigEKECnnSPQ)
From the above visualization, we can clearly see that the longest increasing subsequence is of length '5' -- as shown by dp[4][4]
.
Code
Here is the code for our bottom-up dynamic programming approach:
The time and space complexity of the above algorithm is O(n*m).
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible