The Wayback Machine - https://web.archive.org/web/20240708021639/https://www.geeksforgeeks.org/print-shortest-common-supersequence/
Open In App

Printing Shortest Common Supersequence

Last Updated : 02 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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”, 
 

Shortest Supersequence Problem DP table

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




/* A dynamic programming based C++ program print
   shortest supersequence of two strings */
#include <bits/stdc++.h>
using namespace std;
 
// returns shortest supersequence of X and Y
string printShortestSuperSeq(string X, string Y)
{
    int m = X.length();
    int n = Y.length();
 
    // dp[i][j] contains length of shortest supersequence
    // for X[0..i-1] and Y[0..j-1]
    int dp[m + 1][n + 1];
 
    // Fill table in bottom up manner
    for (int i = 0; i <= m; i++)
    {
        for (int j = 0; j <= n; j++)
        {
            // Below steps follow recurrence relation
            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]);
        }
    }
 
    // Following code is used to print shortest supersequence
 
    // dp[m][n] stores the length of the shortest supersequence
    // of X and Y
     
 
    // string to store the shortest supersequence
    string str;
 
    // Start from the bottom right corner and one by one
    // push characters in output string
    int i = m, j = n;
    while (i > 0 && j > 0)
    {
        // If current character in X and Y are same, then
        // current character is part of shortest supersequence
        if (X[i - 1] == Y[j - 1])
        {
            // Put current character in result
            str.push_back(X[i - 1]);
 
            // reduce values of i, j and index
            i--, j--;
        }
 
        // If current character in X and Y are different
        else if (dp[i - 1][j] > dp[i][j - 1])
        {
            // Put current character of Y in result
            str.push_back(Y[j - 1]);
 
            // reduce values of j and index
            j--;
        }
        else
        {
            // Put current character of X in result
            str.push_back(X[i - 1]);
 
            // reduce values of i and index
            i--;
        }
    }
 
    // If Y reaches its end, put remaining characters
    // of X in the result string
    while (i > 0)
    {
        str.push_back(X[i - 1]);
        i--;
    }
 
    // If X reaches its end, put remaining characters
    // of Y in the result string
    while (j > 0)
    {
        str.push_back(Y[j - 1]);
        j--;
    }
 
    // reverse the string and return it
    reverse(str.begin(), str.end());
    return str;
}
 
// Driver program to test above function
int main()
{
    string X = "AGGTAB";
    string Y = "GXTXAYB";
 
    cout << printShortestSuperSeq(X, Y);
 
    return 0;
}


Java




/* A dynamic programming based Java program print
shortest supersequence of two strings */
class GFG {
 
    // returns shortest supersequence of X and Y
    static String printShortestSuperSeq(String X, String Y)
    {
        int m = X.length();
        int n = Y.length();
 
        // dp[i][j] contains length of
        // shortest supersequence
        // for X[0..i-1] and Y[0..j-1]
        int dp[][] = new int[m + 1][n + 1];
 
        // Fill table in bottom up manner
        for (int i = 0; i <= m; i++)
        {
            for (int j = 0; j <= n; j++)
            {
                 
                // Below steps follow recurrence relation
                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]);
                }
            }
        }
 
        // Following code is used to print
        // shortest supersequence dp[m][n] s
        // tores the length of the shortest
        // supersequence of X and Y
 
        // string to store the shortest supersequence
        String str = "";
 
        // Start from the bottom right corner and one by one
        // push characters in output string
        int i = m, j = n;
        while (i > 0 && j > 0)
         
        {
            // If current character in X and Y are same, then
            // current character is part of shortest supersequence
            if (X.charAt(i - 1) == Y.charAt(j - 1))
             
            {
                // Put current character in result
                str += (X.charAt(i - 1));
 
                // reduce values of i, j and index
                i--;
                j--;
            }
             
            // If current character in X and Y are different
            else if (dp[i - 1][j] > dp[i][j - 1])
            {
                 
                // Put current character of Y in result
                str += (Y.charAt(j - 1));
 
                // reduce values of j and index
                j--;
            }
            else
            {
                 
                // Put current character of X in result
                str += (X.charAt(i - 1));
 
                // reduce values of i and index
                i--;
            }
        }
 
        // If Y reaches its end, put remaining characters
        // of X in the result string
        while (i > 0)
        {
            str += (X.charAt(i - 1));
            i--;
        }
 
        // If X reaches its end, put remaining characters
        // of Y in the result string
        while (j > 0)
        {
            str += (Y.charAt(j - 1));
            j--;
        }
 
        // reverse the string and return it
        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--)
        {
            // Swap values of left and right
            char temp = temparray[left];
            temparray[left] = temparray[right];
            temparray[right] = temp;
        }
        return String.valueOf(temparray);
    }
     
    // Driver code
    public static void main(String[] args)
    {
        String X = "AGGTAB";
        String Y = "GXTXAYB";
        System.out.println(printShortestSuperSeq(X, Y));
    }
}
 
// This code is contributed by 29AjayKumar


Python3




# A dynamic programming based Python3 program print
# shortest supersequence of two strings
 
# returns shortest supersequence of X and Y
 
 
def printShortestSuperSeq(m, n, x, y):
 
    # dp[i][j] contains length of shortest
    # supersequence for X[0..i-1] and Y[0..j-1]
    dp = [[0 for i in range(n + 1)]
          for j in range(m + 1)]
 
    # Fill table in bottom up manner
    for i in range(m + 1):
        for j in range(n + 1):
 
            # Below steps follow recurrence relation
            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])
 
    # Following code is used to print
    # shortest supersequence
 
    # dp[m][n] stores the length of the
    # shortest supersequence of X and Y
 
    # string to store the shortest supersequence
    string = ""
 
    # Start from the bottom right corner and
    # add the characters to the output string
    i = m
    j = n
    while i * j > 0:
 
        # If current character in X and Y are same,
        # then current character is part of
        # shortest supersequence
        if x[i - 1] == y[j - 1]:
 
            # Put current character in result
            string = x[i - 1] + string
 
            # reduce values of i, j and index
            i -= 1
            j -= 1
 
        # If current character in X and Y are different
        elif dp[i - 1][j] > dp[i][j - 1]:
 
            # Put current character of Y in result
            string = y[j - 1] + string
 
            # reduce values of j and index
            j -= 1
        else:
 
            # Put current character of X in result
            string = x[i - 1] + string
 
            # reduce values of i and index
            i -= 1
 
    # If Y reaches its end, put remaining characters
    # of X in the result string
    while i > 0:
        string = x[i - 1] + string
        i -= 1
 
    # If X reaches its end, put remaining characters
    # of Y in the result string
    while j > 0:
        string = y[j - 1] + string
        j -= 1
 
    return string
 
 
# Driver Code
if __name__ == "__main__":
    x = "GXTXAYB"
    y = "AGGTAB"
    m = len(x)
    n = len(y)
 
    # Take the smaller string as x and larger one as y
    if m > n:
        x, y = y, x
        m, n = n, m
 
    print(*printShortestSuperSeq(m, n, x, y))
 
# This code is contributed by
# sanjeev2552


C#




/* A dynamic programming based C# program print
shortest supersequence of two strings */
using System;
 
class GFG
{
 
    // returns shortest supersequence of X and Y
    static String printShortestSuperSeq(String X, String Y)
    {
        int m = X.Length;
        int n = Y.Length;
 
        // dp[i,j] contains length of
        // shortest supersequence
        // for X[0..i-1] and Y[0..j-1]
        int [,]dp = new int[m + 1, n + 1];
        int i, j;
         
        // Fill table in bottom up manner
        for (i = 0; i <= m; i++)
        {
            for (j = 0; j <= n; j++)
            {
                 
                // Below steps follow recurrence relation
                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]);
                }
            }
        }
 
        // Following code is used to print
        // shortest supersequence dp[m,n] s
        // tores the length of the shortest
        // supersequence of X and Y
 
        // string to store the shortest supersequence
        String str = "";
 
        // Start from the bottom right corner and one by one
        // push characters in output string
        i = m; j = n;
        while (i > 0 && j > 0)
         
        {
            // If current character in X and Y are same, then
            // current character is part of shortest supersequence
            if (X[i - 1] == Y[j - 1])
             
            {
                // Put current character in result
                str += (X[i - 1]);
 
                // reduce values of i, j and index
                i--;
                j--;
            }
             
            // If current character in X and Y are different
            else if (dp[i - 1, j] > dp[i, j - 1])
            {
                 
                // Put current character of Y in result
                str += (Y[j - 1]);
 
                // reduce values of j and index
                j--;
            }
            else
            {
                 
                // Put current character of X in result
                str += (X[i - 1]);
 
                // reduce values of i and index
                i--;
            }
        }
 
        // If Y reaches its end, put remaining characters
        // of X in the result string
        while (i > 0)
        {
            str += (X[i - 1]);
            i--;
        }
 
        // If X reaches its end, put remaining characters
        // of Y in the result string
        while (j > 0)
        {
            str += (Y[j - 1]);
            j--;
        }
 
        // reverse the string and return it
        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--)
        {
            // Swap values of left and right
            char temp = temparray[left];
            temparray[left] = temparray[right];
            temparray[right] = temp;
        }
        return String.Join("",temparray);
    }
     
    // Driver code
    public static void Main(String[] args)
    {
        String X = "AGGTAB";
        String Y = "GXTXAYB";
        Console.WriteLine(printShortestSuperSeq(X, Y));
    }
}
 
/* This code has been contributed
by PrinciRaj1992*/


Javascript




<script>
 
/* A dynamic programming based Javascript program print
shortest supersequence of two strings */
 
// returns shortest supersequence of X and Y
function printShortestSuperSeq(X,Y)
{
         let m = X.length;
        let n = Y.length;
   
        // dp[i][j] contains length of
        // shortest supersequence
        // for X[0..i-1] and Y[0..j-1]
        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;
        }
   
        // Fill table in bottom up manner
        for (let i = 0; i <= m; i++)
        {
            for (let j = 0; j <= n; j++)
            {
                   
                // Below steps follow recurrence relation
                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]);
                }
            }
        }
   
        // Following code is used to print
        // shortest supersequence dp[m][n] s
        // tores the length of the shortest
        // supersequence of X and Y
   
        // string to store the shortest supersequence
        let str = "";
   
        // Start from the bottom right corner and one by one
        // push characters in output string
        let i = m, j = n;
        while (i > 0 && j > 0)
           
        {
            // If current character in X and Y are same, then
            // current character is part of shortest supersequence
            if (X[i-1] == Y[j-1])
               
            {
                // Put current character in result
                str += (X[i-1]);
   
                // reduce values of i, j and index
                i--;
                j--;
            }
               
            // If current character in X and Y are different
            else if (dp[i - 1][j] > dp[i][j - 1])
            {
                   
                // Put current character of Y in result
                str += (Y[j-1]);
   
                // reduce values of j and index
                j--;
            }
            else
            {
                   
                // Put current character of X in result
                str += (X[i-1]);
   
                // reduce values of i and index
                i--;
            }
        }
   
        // If Y reaches its end, put remaining characters
        // of X in the result string
        while (i > 0)
        {
            str += (X[i-1]);
            i--;
        }
   
        // If X reaches its end, put remaining characters
        // of Y in the result string
        while (j > 0)
        {
            str += (Y[j-1]);
            j--;
        }
   
        // reverse the string and return it
        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--)
        {
            // Swap values of left and right
            let temp = temparray[left];
            temparray[left] = temparray[right];
            temparray[right] = temp;
        }
        return (temparray).join("");
}
 
// Driver code
let X = "AGGTAB";
let Y = "GXTXAYB";
document.write(printShortestSuperSeq(X, Y));
 
 
// This code is contributed by rag2127
 
</script>


Output

AGXGTXAYB

Time complexity of above solution is O(n2). 
Auxiliary space used by the program is O(n2).


 



Previous Article
Next Article

Similar Reads

POTD Solutions | 13 Nov’ 23 | Shortest Common Supersequence
View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Dynamic Programming but will also help you build up problem-solving skills. We recommend you to try this
6 min read
Shortest Common Supersequence
Given two strings str1 and str2, the task is to find the length of the shortest string that has both str1 and str2 as subsequences. Examples : Input: str1 = "geek", str2 = "eke" Output: 5 Explanation: String "geeke" has both string "geek" and "eke" as subsequences. Input: str1 = "AGGTAB", str2 = "GXTXAYB" Output: 9 Explanation: String "AGXGTXAYB" h
15+ min read
Shortest Supersequence by Sequence Reconstruction
Given an array arr[] and a 2D array sequences[], determine if arr is the shortest possible and the only supersequence for the given sequences. A supersequence is a sequence that has all sequences[i] as subsequences. The task is to return true if arr is the only shortest supersequence for sequences, or false otherwise. Example: Input: arr = {1,2,3},
8 min read
Printing Longest Common Subsequence | Set 2 (Printing All)
Given two sequences, print all longest subsequence present in both of them.Examples: Input: string X = "AGTGATG" string Y = "GTTAG" Output: GTAG GTTG Input: string X = "AATCC" string Y = "ACACG" Output: ACC AAC Input: string X = "ABCBDAB" string Y = "BDCABA" Output: BCAB BCBA BDAB We have discussed Longest Common Subsequence (LCS) problem here. The
12 min read
Printing Paths in Dijkstra's Shortest Path Algorithm
Given a graph and a source vertex in the graph, find the shortest paths from the source to all vertices in the given graph.We have discussed Dijkstra's Shortest Path algorithm in the below posts.  Dijkstra’s shortest path for adjacency matrix representationDijkstra’s shortest path for adjacency list representationThe implementations discussed above
15 min read
Printing Longest Common Subsequence
Given two sequences, print the longest subsequence present in both of them. Examples: LCS for input Sequences “ABCDGH” and “AEDFHR” is “ADH” of length 3. LCS for input Sequences “AGGTAB” and “GXTXAYB” is “GTAB” of length 4.We have discussed Longest Common Subsequence (LCS) problem in a previous post. The function discussed there was mainly to find
15+ min read
Time saved travelling in shortest route and shortest path through given city
Given a matrix mat[][] of size N * N, where mat[i][j] represents the time taken to reach from ith city to jth city. Also, given M queries in the form of three arrays S[], I[], and D[] representing source, intermediate, and destination respectively. The task is to find the time taken to go from S[i] to D[i] using I[i] city and the time that can be s
10 min read
Difference between the shortest and second shortest path in an Unweighted Bidirectional Graph
Given an unweighted bidirectional graph containing N nodes and M edges represented by an array arr[][2]. The task is to find the difference in length of the shortest and second shortest paths from node 1 to N. If the second shortest path does not exist, print 0. Note: The graph is connected, does not contain multiple edges and self loops. (2&lt;=N
15+ min read
Sum of shortest distance on source to destination and back having at least a common vertex
Given a directed weighted graph and the source and destination vertex. The task is to find the sum of shortest distance on the path going from source to destination and then from destination to source such that both the paths have at least a common vertex other than the source and the destination. Note: On going from destination to source, all the
15+ min read
Check if given Arrays have a Unique Shortest Common Super-sequence
Given a 2D array arr[][] of size N*M which denotes N arrays, each of size M. The task is to check if all these arrays have a unique common supersequence. Examples: Input: N = 2, M = 2, arr[][] = { { 1, 2 }, {1, 3 } }Output: FalseExplanation: There are two possible supersequence: {1, 3, 2 } and {1, 2, 3}. Input: N = 3, M = 2, arr[][] = { { 1, 2 }, {
10 min read
three90RightbarBannerImg