Customer Support: firstname.lastname@example.org
We are listening to your every feedback,
and taking action to constantly improve your learning experience.
If you have any feedback, please use this form:
- If you are a student, email to email@example.com to get special student discount.
Pre-requisite:Longest Common Subsequence
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If multiple answers exist, you may return any of them.
A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.
Input: str1 = "abac", str2 = "cab"
str1 = "abac" is a subsequence of "cabac" because we can delete the first "c".
str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac".
The answer provided is the shortest such string that satisfies these properties.
The logic is simple, if you already know how to compute Longest Common Subsequence (LCS) of two strings. To compute supersequence we compute LCS of the given two strings first. In the supersequence: apart from the LCS, other characters also need to be present, in the order they are present in the given two strings, keeping in mind the definition of the subsequence.
This is a Premium content. Please subscribe to access the code.
After subscribing please come back and refresh this page.
The above content is written by:
If you have any feedback, please use this form: https://thealgorists.com/Feedback.
Follow Us On LinkedIn