<p>Given two strings <code>str1</code> and <code>str2</code>, return <em>the shortest string that has both </em><code>str1</code><em> and </em><code>str2</code><em> as <strong>subsequences</strong></em>. If there are multiple valid strings, return <strong>any</strong> of them.</p>

<p>A string <code>s</code> is a <strong>subsequence</strong> of string <code>t</code> if deleting some number of characters from <code>t</code> (possibly <code>0</code>) results in the string <code>s</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> str1 = &quot;abac&quot;, str2 = &quot;cab&quot;
<strong>Output:</strong> &quot;cabac&quot;
<strong>Explanation:</strong> 
str1 = &quot;abac&quot; is a subsequence of &quot;cabac&quot; because we can delete the first &quot;c&quot;.
str2 = &quot;cab&quot; is a subsequence of &quot;cabac&quot; because we can delete the last &quot;ac&quot;.
The answer provided is the shortest such string that satisfies these properties.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> str1 = &quot;aaaaaaaa&quot;, str2 = &quot;aaaaaaaa&quot;
<strong>Output:</strong> &quot;aaaaaaaa&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= str1.length, str2.length &lt;= 1000</code></li>
	<li><code>str1</code> and <code>str2</code> consist of lowercase English letters.</li>
</ul>

class Solution:
    def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
        # longest common subseq between s1[i:] and s2[j:]
        @cache
        def dp(i, j):
            if i == len(str1) or j == len(str2):
                return 0, None
            
            maxLen = 0
            optimalNext = None
            skip1 = dp(i + 1, j)
            skip2 = dp(i, j + 1)
            if skip1[0] > skip2[0]:
                maxLen = skip1[0]
                optimalNext = (i + 1, j)
            else:
                maxLen = skip2[0]
                optimalNext = (i, j + 1)
            
            if str1[i] == str2[j]:
                use = dp(i + 1, j + 1)
                if 1 + use[0] > maxLen:
                    maxLen = 1 + use[0]
                    optimalNext = (i + 1, j + 1)
            
            return maxLen, optimalNext
        
        maxLen, nextIdx = dp(0, 0)
        equalIndices = []
        if str1[0] == str2[0]:
            # this helps the for-loop below run correctly
            equalIndices.append((0, 0))

        while True:
            i, j = nextIdx
            if i == len(str1) or j == len(str2): break
            if str1[i] == str2[j]:
                equalIndices.append(nextIdx)
            # print(nextIdx, str1[i], str2[j])
            _, nextIdx = dp(i, j)
        
        ret = [] # chars
        previ, prevj = -1, -1
        for curi, curj in equalIndices:
            # chars from either s1 or s2, order doesn't matter
            for i in range(previ + 1, curi):
                ret.append(str1[i])
            for j in range(prevj + 1, curj):
                ret.append(str2[j])
            
            # shared char
            ret.append(str1[curi])

            previ, prevj = curi, curj
        
        # final chars after longest subseq is done
        for i in range(previ + 1, len(str1)):
            ret.append(str1[i])
        for j in range(prevj + 1, len(str2)):
            ret.append(str2[j])

        return ''.join(ret)
289674