Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If there are multiple valid strings, return any of them. A string s is a subsequence of string t if deleting some number of characters from t (possibly 0) results in the string s.
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If there are multiple valid strings, return any of them. A string s is a subsequence of string t if deleting some number of characters from t (possibly 0) results in the string s.
Example 1:
Input: {"str1":"abac","str2":"cab"}
Output: cabac
Constraints:
1≤str1.length,str2.length≤1000
str1 and str2 consist of lowercase English letters.
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"str1":"abac","str2":"cab"}
First find the longest common subsequence (LCS). Letters in it can be written once and shared; every other letter must be written separately. dp[i][j] = LCS of the first i letters of str1 and the first j of str2.