<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_dfs(self, str1: str, str2: str) -> str:
        # number of status: m * n
        # time cost of each status: (m + n) for concatenation !can be optimized
        # space cost of each status: (m + n) for concatenation !can be optimized
        # Time: O(mn(m + n))
        # Space: O(mn(m + n))
        n = len(str1)
        m = len(str2)

        @cache
        def dfs(i, j):
            if i == n:
                return str2[j:]
            if j == m:
                return str1[i:]
            if str1[i] == str2[j]:
                return str1[i] + dfs(i + 1, j + 1)
            
            res_1 = str1[i] + dfs(i + 1, j)
            res_2 = str2[j] + dfs(i, j + 1)
            if len(res_1) < len(res_2):
                return res_1
            return res_2
        
        return dfs(0, 0)
    
    def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
        # number of status: m * n
        # time cost of each status: O(1)
        # space cost of each status: O(1)
        # Time: O(mn)
        # Space: O(mn)
        n = len(str1)
        m = len(str2)

        @cache
        def dfs(i, j):
            if i == n:
                return m - j
            if j == m:
                return n - i
            if str1[i] == str2[j]:
                return 1 + dfs(i + 1, j + 1)
            return min(dfs(i + 1, j), dfs(i, j + 1)) + 1
            
        def make_string(i, j):
            if i == n:
                return str2[j:]
            if j == m:
                return str1[i:]
            if str1[i] == str2[j]:
                return str1[i] + make_string(i + 1, j + 1)
            # 末尾的递归是 if-else
            # make_ans(i - 1, j) 和 make_ans(i, j - 1) 不会都调用
            # 所以 make_ans 的递归树仅仅是一条链
            if dfs(i, j) == dfs(i + 1, j) + 1:
                return str1[i] + make_string(i + 1, j)
            return str2[j] + make_string(i, j + 1)
        
        return make_string(0, 0)


class Solution_dp:
    def shortestCommonSupersequence(self, s: str, t: str) -> str:
        n, m = len(s), len(t)
        # f[i+1][j+1] 表示 s 的前 i 个字母和 t 的前 j 个字母的最短公共超序列的长度
        f = [[0] * (m + 1) for _ in range(n + 1)]
        f[0] = list(range(m + 1))  # 递归边界 f[0][j] = j
        for i in range(1, n + 1):
            f[i][0] = i  # 递归边界
        for i, x in enumerate(s):
            for j, y in enumerate(t):
                if x == y:  # 最短公共超序列一定包含 s[i]
                    f[i + 1][j + 1] = f[i][j] + 1
                else:  # 取更短的组成答案
                    f[i + 1][j + 1] = min(f[i][j + 1], f[i + 1][j]) + 1

        ans = []
        i, j = n - 1, m - 1
        while i >= 0 and j >= 0:
            if s[i] == t[j]:  # 公共超序列一定包含 s[i]
                ans.append(s[i])
                i -= 1
                j -= 1  # 相当于继续递归 make_ans(i - 1, j - 1)
            elif f[i + 1][j + 1] == f[i][j + 1] + 1:
                ans.append(s[i])
                i -= 1  # 相当于继续递归 make_ans(i - 1, j)
            else:
                ans.append(t[j])
                j -= 1  # 相当于继续递归 make_ans(i, j - 1)

        # 补上前面的递归边界
        # reversed(ans) 也可以写成 ans[::-1]
        return s[:i + 1] + t[:j + 1] + ''.join(reversed(ans))


176715