Given two strings X and Y, print the shortest string that has both X and Y as subsequences. If multiple shortest supersequence exists, print any one of them.
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 supersequence for two given strings here. In this post, we print the shortest supersequence.
We have already discussed below algorithm to find length of shortest supersequence 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 supersequence 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 –
Time complexity of above solution is O(n2).
Auxiliary space used by the program is O(n2).
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Shortest Common Supersequence
- Printing Longest Common Subsequence | Set 2 (Printing All)
- Printing Longest Common Subsequence
- Printing Items in 0/1 Knapsack
- Printing Longest Bitonic Subsequence
- Printing Maximum Sum Increasing Subsequence
- Printing longest Increasing consecutive subsequence
- Printing brackets in Matrix Chain Multiplication Problem
- Printing Matrix Chain Multiplication (A Space Optimized Solution)
- Shortest possible combination of two strings
- Shortest Uncommon Subsequence
- Multistage Graph (Shortest Path)
- Shortest path with exactly k edges in a directed and weighted graph
- Longest Common Subsequence | DP-4
- Longest Common Substring | DP-29