Given two strings str1 and str2, return the shortest string that has both str1and 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”
Explanation:
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.
Solution:
Calculate the dp table (same as Tabulation ) and We’ll make our
answer from dp[n1][n2] and trace back till one of the row or column reaches it’s limit.