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.
function findShortestCommonSupersequence(str1, str2) {
// If either of the strings is empty,
// return the other string as the result.
if (str1.length === 0) {
return str2;
}
if (str2.length === 0) {
return str1;
}
// Check if the last characters of
// both strings are the same.
if (str1[str1.length - 1] === str2[str2.length - 1]) {
// If they are, recursively find the SCS
// without the last characters and
// append the common character.
return findShortestCommonSupersequence
(str1.slice(0, -1), str2.slice(0, -1))
+ str1[str1.length - 1];
}
// If the last characters are different,
// explore both possibilities and choose
// the shorter one.
const result1 = findShortestCommonSupersequence
(str1.slice(0, -1), str2);
const result2 = findShortestCommonSupersequence
(str1, str2.slice(0, -1));
// Return the result with the shorter length.
return result1.length < result2.length ? result1 +
str1[str1.length - 1] : result2 + str2[str2.length - 1];
} // Example usage: const str1 = "ABAC" ;
const str2 = "CAB" ;
const shortestCommonSupersequence = findShortestCommonSupersequence(str1, str2);
console.log(shortestCommonSupersequence); |
CABAC
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.
function findShortestSuperSequence(str1, str2) {
// Determine the lengths of the input strings
let m = str1.length;
let n = str2.length;
// Create a dynamic programming table
// to store subproblem results
let dpTable = new Array(m + 1).
fill( null ).map(() =>
new Array(n + 1).fill(0));
// Fill in the DP table to
// calculate the length of LCS
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]);
}
}
}
// Initialize pointers and an empty
// string for the supersequence
let i = m;
let j = n;
let supersequence = "" ;
// Reconstruct the 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--;
}
}
}
// Add any remaining characters from both strings
while (i > 0) {
supersequence = str1[i - 1] + supersequence;
i--;
}
while (j > 0) {
supersequence = str2[j - 1] + supersequence;
j--;
}
return supersequence;
} // Example usage: let str1 = "abac" ;
let str2 = "cab" ;
let shortestSuperSequence = findShortestSuperSequence(str1, str2);
console.log(shortestSuperSequence); |
cabac
Time Complexity: O(m*n)
Space Complexity: O(m*n)