Printing Shortest Common Supersequence
Last Updated :
02 Jan, 2024
Given two strings X and Y, print the shortest string that has both X and Y as subsequences. If multiple shortest super-sequence exists, print any one of them.
Examples:Â
Â
Input: X = "AGGTAB", Y = "GXTXAYB"
Output: "AGXGTXAYB" OR "AGGXTXAYB"
OR Any string that represents shortest
supersequence of X and Y
Input: X = "HELLO", Y = "GEEK"
Output: "GEHEKLLO" OR "GHEEKLLO"
OR Any string that represents shortest
supersequence of X and Y
Â
We have discussed how to print length of shortest possible super-sequence for two given strings here. In this post, we print the shortest super-sequence.
We have already discussed below algorithm to find length of shortest super-sequence in previous post-
Â
Let X[0..m-1] and Y[0..n-1] be two strings and m and be respective
lengths.
if (m == 0) return n;
if (n == 0) return m;
// If last characters are same, then add 1 to result and
// recur for X[]
if (X[m-1] == Y[n-1])
return 1 + SCS(X, Y, m-1, n-1);
// Else find shortest of following two
// a) Remove last character from X and recur
// b) Remove last character from Y and recur
else return 1 + min( SCS(X, Y, m-1, n), SCS(X, Y, m, n-1) );
The following table shows steps followed by the above algorithm if we solve it in bottom-up manner using Dynamic Programming for strings X = “AGGTAB” and Y = “GXTXAYB”,Â
Â
Using the DP solution matrix, we can easily print shortest super-sequence of two strings by following below steps –
Â
We start from the bottom-right most cell of the matrix and
push characters in output string based on below rules-
1. If the characters corresponding to current cell (i, j)
in X and Y are same, then the character is part of shortest
supersequence. We append it in output string and move
diagonally to next cell (i.e. (i - 1, j - 1)).
2. If the characters corresponding to current cell (i, j)
in X and Y are different, we have two choices -
If matrix[i - 1][j] > matrix[i][j - 1],
we add character corresponding to current
cell (i, j) in string Y in output string
and move to the left cell i.e. (i, j - 1)
else
we add character corresponding to current
cell (i, j) in string X in output string
and move to the top cell i.e. (i - 1, j)
3. If string Y reaches its end i.e. j = 0, we add remaining
characters of string X in the output string
else if string X reaches its end i.e. i = 0, we add
remaining characters of string Y in the output string.
Below is the implementation of above idea –Â
Â
C++14
#include <bits/stdc++.h>
using namespace std;
string printShortestSuperSeq(string X, string Y)
{
int m = X.length();
int n = Y.length();
int dp[m + 1][n + 1];
for ( int i = 0; i <= m; i++)
{
for ( int j = 0; j <= n; j++)
{
if (i == 0)
dp[i][j] = j;
else if (j == 0)
dp[i][j] = i;
else if (X[i - 1] == Y[j - 1])
dp[i][j] = 1 + dp[i - 1][j - 1];
else
dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1]);
}
}
string str;
int i = m, j = n;
while (i > 0 && j > 0)
{
if (X[i - 1] == Y[j - 1])
{
str.push_back(X[i - 1]);
i--, j--;
}
else if (dp[i - 1][j] > dp[i][j - 1])
{
str.push_back(Y[j - 1]);
j--;
}
else
{
str.push_back(X[i - 1]);
i--;
}
}
while (i > 0)
{
str.push_back(X[i - 1]);
i--;
}
while (j > 0)
{
str.push_back(Y[j - 1]);
j--;
}
reverse(str.begin(), str.end());
return str;
}
int main()
{
string X = "AGGTAB" ;
string Y = "GXTXAYB" ;
cout << printShortestSuperSeq(X, Y);
return 0;
}
|
Java
class GFG {
static String printShortestSuperSeq(String X, String Y)
{
int m = X.length();
int n = Y.length();
int dp[][] = new int [m + 1 ][n + 1 ];
for ( int i = 0 ; i <= m; i++)
{
for ( int j = 0 ; j <= n; j++)
{
if (i == 0 )
{
dp[i][j] = j;
}
else if (j == 0 )
{
dp[i][j] = i;
}
else if (X.charAt(i - 1 ) == Y.charAt(j - 1 ))
{
dp[i][j] = 1 + dp[i - 1 ][j - 1 ];
}
else
{
dp[i][j] = 1 + Math.min(dp[i - 1 ][j], dp[i][j - 1 ]);
}
}
}
String str = "" ;
int i = m, j = n;
while (i > 0 && j > 0 )
{
if (X.charAt(i - 1 ) == Y.charAt(j - 1 ))
{
str += (X.charAt(i - 1 ));
i--;
j--;
}
else if (dp[i - 1 ][j] > dp[i][j - 1 ])
{
str += (Y.charAt(j - 1 ));
j--;
}
else
{
str += (X.charAt(i - 1 ));
i--;
}
}
while (i > 0 )
{
str += (X.charAt(i - 1 ));
i--;
}
while (j > 0 )
{
str += (Y.charAt(j - 1 ));
j--;
}
str = reverse(str);
return str;
}
static String reverse(String input)
{
char [] temparray = input.toCharArray();
int left, right = 0 ;
right = temparray.length - 1 ;
for (left = 0 ; left < right; left++, right--)
{
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.valueOf(temparray);
}
public static void main(String[] args)
{
String X = "AGGTAB" ;
String Y = "GXTXAYB" ;
System.out.println(printShortestSuperSeq(X, Y));
}
}
|
Python3
def printShortestSuperSeq(m, n, x, y):
dp = [[ 0 for i in range (n + 1 )]
for j in range (m + 1 )]
for i in range (m + 1 ):
for j in range (n + 1 ):
if i = = 0 :
dp[i][j] = j
elif j = = 0 :
dp[i][j] = i
elif x[i - 1 ] = = y[j - 1 ]:
dp[i][j] = 1 + dp[i - 1 ][j - 1 ]
else :
dp[i][j] = 1 + min (dp[i - 1 ][j],
dp[i][j - 1 ])
string = ""
i = m
j = n
while i * j > 0 :
if x[i - 1 ] = = y[j - 1 ]:
string = x[i - 1 ] + string
i - = 1
j - = 1
elif dp[i - 1 ][j] > dp[i][j - 1 ]:
string = y[j - 1 ] + string
j - = 1
else :
string = x[i - 1 ] + string
i - = 1
while i > 0 :
string = x[i - 1 ] + string
i - = 1
while j > 0 :
string = y[j - 1 ] + string
j - = 1
return string
if __name__ = = "__main__" :
x = "GXTXAYB"
y = "AGGTAB"
m = len (x)
n = len (y)
if m > n:
x, y = y, x
m, n = n, m
print ( * printShortestSuperSeq(m, n, x, y))
|
C#
using System;
class GFG
{
static String printShortestSuperSeq(String X, String Y)
{
int m = X.Length;
int n = Y.Length;
int [,]dp = new int [m + 1, n + 1];
int i, j;
for (i = 0; i <= m; i++)
{
for (j = 0; j <= n; j++)
{
if (i == 0)
{
dp[i, j] = j;
}
else if (j == 0)
{
dp[i, j] = i;
}
else if (X[i - 1] == Y[j - 1])
{
dp[i, j] = 1 + dp[i - 1, j - 1];
}
else
{
dp[i, j] = 1 + Math.Min(dp[i - 1, j], dp[i, j - 1]);
}
}
}
String str = "" ;
i = m; j = n;
while (i > 0 && j > 0)
{
if (X[i - 1] == Y[j - 1])
{
str += (X[i - 1]);
i--;
j--;
}
else if (dp[i - 1, j] > dp[i, j - 1])
{
str += (Y[j - 1]);
j--;
}
else
{
str += (X[i - 1]);
i--;
}
}
while (i > 0)
{
str += (X[i - 1]);
i--;
}
while (j > 0)
{
str += (Y[j - 1]);
j--;
}
str = reverse(str);
return str;
}
static String reverse(String input)
{
char [] temparray = input.ToCharArray();
int left, right = 0;
right = temparray.Length - 1;
for (left = 0; left < right; left++, right--)
{
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.Join( "" ,temparray);
}
public static void Main(String[] args)
{
String X = "AGGTAB" ;
String Y = "GXTXAYB" ;
Console.WriteLine(printShortestSuperSeq(X, Y));
}
}
|
Javascript
<script>
function printShortestSuperSeq(X,Y)
{
let m = X.length;
let n = Y.length;
let dp = new Array(m + 1);
for (let i=0;i<(m+1);i++)
{
dp[i]= new Array(n+1);
for (let j=0;j<(n+1);j++)
dp[i][j]=0;
}
for (let i = 0; i <= m; i++)
{
for (let j = 0; j <= n; j++)
{
if (i == 0)
{
dp[i][j] = j;
}
else if (j == 0)
{
dp[i][j] = i;
}
else if (X[i-1] == Y[j-1])
{
dp[i][j] = 1 + dp[i - 1][j - 1];
}
else
{
dp[i][j] =
1 + Math.min(dp[i - 1][j], dp[i][j - 1]);
}
}
}
let str = "" ;
let i = m, j = n;
while (i > 0 && j > 0)
{
if (X[i-1] == Y[j-1])
{
str += (X[i-1]);
i--;
j--;
}
else if (dp[i - 1][j] > dp[i][j - 1])
{
str += (Y[j-1]);
j--;
}
else
{
str += (X[i-1]);
i--;
}
}
while (i > 0)
{
str += (X[i-1]);
i--;
}
while (j > 0)
{
str += (Y[j-1]);
j--;
}
str = reverse(str);
return str;
}
function reverse(input)
{
let temparray = input.split( "" );
let left, right = 0;
right = temparray.length - 1;
for (left = 0; left < right; left++, right--)
{
let temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return (temparray).join( "" );
}
let X = "AGGTAB" ;
let Y = "GXTXAYB" ;
document.write(printShortestSuperSeq(X, Y));
</script>
|
Time complexity of above solution is O(n2).Â
Auxiliary space used by the program is O(n2).
Â
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...