Find the shortest string that contains two given strings as subsequences.
Given two strings str1 and str2, return the shortest common supersequence (SCS) of the two strings.
A string is a supersequence of another string if it can be formed by inserting characters anywhere in the original string without changing the order of the existing characters.
Your task is to construct a string with minimum possible length such that both str1 and str2 are subsequences of it.
If there are multiple answers, you may return any one of them.
str1 and str2.str1 and str2 as a string.1 <= str1.length, str2.length <= 1000str1 and str2 contain only lowercase English lettersExample 1
Input
str1 = "abac" str2 = "cab"
Output
"cabac"
Explanation
Both strings are subsequences of "cabac", and no shorter valid supersequence exists.
Example 2
Input
str1 = "geek" str2 = "eke"
Output
"geeke"
Explanation
"geeke" contains both "geek" and "eke" as subsequences and has minimum length.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.