![]() |
VOOZH | about |
Given two strings s1 and s2, find the shortest string which has both s1 and s2 as its sub-sequences. If multiple shortest super-sequence exists, print any one of them.
Examples:
Input: s1 = "geek", s2 = "eke"
Output: geeke
Explanation: String "geeke" has both string "geek" and "eke" as subsequences.
Input: s1 = "AGGTAB", s2 = "GXTXAYB"
Output: AGXGTXAYB
Explanation: String "AGXGTXAYB" has both string "AGGTAB" and "GXTXAYB" as subsequences.
The idea is to find the shortest common supersequence (SCS) of two strings by using a dynamic programming (DP) table to calculate the minimum length of the merged string while preserving character order. It builds the SCS by backtracking from the DP table, adding common characters when they match and choosing the smaller cost path when they don’t. Finally, the result is reversed as it’s constructed backward.
To solve the problem, we use a DP table where dp[i][j] represents the length of the shortest common supersequence (SCS) for the first i characters of s1 and the first j characters of s2.
Base Cases:
- dp[0][j]: If s1 is empty, SCS is all characters of s2, so dp[0][j] = j.
- dp[i][0]: If s2 is empty, SCS is all characters of s1, so dp[i][0] = i.
Filling the Table:
Case 1: If s1[i-1] == s2[j-1]:
- When the current characters of s1 and s2 are the same, they must appear only once in the SCS. So, we add 1 to the result of the SCS for the previous characters (dp[i-1][j-1]).
Case 2: If s1[i-1] != s2[j-1]:
- When the current characters differ, we have two choices:
- Include the character from s1 (add 1 to dp[i-1][j]), which means the SCS considers this character of s1.
- Include the character from s2 (add 1 to dp[i][j-1]), which means the SCS considers this character of s2. We take the minimum of these two, as we want the shortest sequence.
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”,
The DP table is filled row by row, starting from dp[0][0] up to dp[m][n], where m and n are the lengths of s1 and s2. After filling the table, dp[m][n] contains the length of the shortest common supersequence.
dp[m][n]), representing the SCS for the full strings s1 and s2.s1[i-1] == s2[j-1], the current character is part of the SCS.i--, j--).dp[i-1][j] and dp[i][j-1] to find the smaller value.dp[i-1][j] < dp[i][j-1], include s1[i-1] in the result and move up (i--).s2[j-1] and move left (j--).i == 0, append the remaining characters of s2 to the result.j == 0, append the remaining characters of s1 to the result.AGGXTXAYB
Time Complexity: O(|str1|*|str2|), as we are running nested loop which is iterating |str1|*|str2| times.
Auxiliary Space: O(|str1|*|str2|), as we are using dp array of size |str1|*|str2|.