JavaScript Program for Printing Shortest Common Supersequence
Last Updated :
20 Nov, 2023
A Shortest Common Supersequence (SCS) is the shortest or smallest string that contains two given strings as a subsequence. It is a minimal combination of characters that includes all elements of both input strings. In this article, we will see different approaches for printing the shortest common supersequence in JavaScript.
Example:
Input: A = "ABAC", B = "CAB"
Output: CABAC
There are two different approaches to printing the Shortest Common Subsequence in JavaScript. Let’s discuss each one of them.
Method 1: Using Recursive Approach
- Start by handling the base cases. If either of the input strings is empty, return the other string as the SCS.
- Now check if the last characters of both strings are the same. If they are, recursively find the SCS without these characters and append the common character.
- If the last characters are different, explore two possibilities. Find the SCS by removing the last character from one string and then the other. Choose the shorter SCS from these two options.
- Now return the chosen SCS, which is either the one without the last character from the first string or the one without the last character from the second string, depending on which one is shorter.
Example: In this example, we will print the Shortest Common Supersequence using a Recursive approach.
Javascript
function findShortestCommonSupersequence(str1, str2) {
if (str1.length === 0) {
return str2;
}
if (str2.length === 0) {
return str1;
}
if (str1[str1.length - 1] === str2[str2.length - 1]) {
return findShortestCommonSupersequence
(str1.slice(0, -1), str2.slice(0, -1))
+ str1[str1.length - 1];
}
const result1 = findShortestCommonSupersequence
(str1.slice(0, -1), str2);
const result2 = findShortestCommonSupersequence
(str1, str2.slice(0, -1));
return result1.length < result2.length ? result1 +
str1[str1.length - 1] : result2 + str2[str2.length - 1];
}
const str1 = "ABAC" ;
const str2 = "CAB" ;
const shortestCommonSupersequence =
findShortestCommonSupersequence(str1, str2);
console.log(shortestCommonSupersequence);
|
Time Complexity: O( 2^(m+n))
Space Complexity: O(m + n)
Approach 2: Using Dynamic Programming
- Create a DP Table to keep track of the Shortest Common Supersequence (SCS) lengths. This table will help us solve the subproblems systematically.
- Now fill the table through both input strings, comparing characters along the way. As you do this, update the table to record how the lengths of the SCS change as you move forward.
- Once the SCS table is filled, now backtrack and follow a path through the table that will enable the reconstruction of the SCS.
- Now return your answer.
Example: In this example, we will print Shortest Common Supersequence using DP approach.
Javascript
function findShortestSuperSequence(str1, str2) {
let m = str1.length;
let n = str2.length;
let dpTable = new Array(m + 1).
fill( null ).map(() =>
new Array(n + 1).fill(0));
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (str1[i - 1] === str2[j - 1]) {
dpTable[i][j] = 1 + dpTable[i - 1][j - 1];
} else {
dpTable[i][j] = Math.max(dpTable[i][j - 1],
dpTable[i - 1][j]);
}
}
}
let i = m;
let j = n;
let supersequence = "" ;
while (i > 0 && j > 0) {
if (str1[i - 1] === str2[j - 1]) {
supersequence = str1[i - 1] + supersequence;
i--;
j--;
} else {
if (dpTable[i - 1][j] > dpTable[i][j - 1]) {
supersequence = str1[i - 1] + supersequence;
i--;
} else {
supersequence = str2[j - 1] + supersequence;
j--;
}
}
}
while (i > 0) {
supersequence = str1[i - 1] + supersequence;
i--;
}
while (j > 0) {
supersequence = str2[j - 1] + supersequence;
j--;
}
return supersequence;
}
let str1 = "abac" ;
let str2 = "cab" ;
let shortestSuperSequence =
findShortestSuperSequence(str1, str2);
console.log(shortestSuperSequence);
|
Time Complexity: O(m*n)
Space Complexity: O(m*n)
Share your thoughts in the comments
Please Login to comment...