13. Shortest Common Supersequence
The problem can be found at the following link: Question Link
My Approach
The idea behind this question is to find the longest common part in two sequences. The longest string we can make is of size m+n, but when we take away the common subsequence part, we get the smallest possible string after putting them together in the same order.
To find the shortest common supersequence of two strings X
and Y
,
I use the concept of the Longest Common Subsequence (LCS).
The idea is to find the LCS of the two strings and then subtract it from the sum of the lengths of the two strings.
Time and Auxiliary Space Complexity
Time Complexity:
O(m * n)
, where m and n are the lengths of stringsX
andY
, respectively.Auxiliary Space Complexity:
O(m * n)
, wherem
andn
are the lengths of stringsX
andY
, respectively.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.
Last updated