<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:

        # largest_overlap = 0
        # shortest_superseq = str1 + str2

        # m = len(str1)
        # n = len(str2)

        # if m > n:
        #     str1, str2 = str2, str1
        #     m, n = n, m

        # for right_bound in range(m + n - 1):
        #     if right_bound < n:
        #         overlap = min(m, right_bound + 1)
        #         for i in range(overlap):
        #             if str1[-(i + 1)] != str2[right_bound - i]:
        #                 break
        #         else:
        #             if overlap > largest_overlap:
        #                 largest_overlap = overlap
        #                 shortest_superseq = str1[:-overlap] + str2
        #     else:
        #         overlap = m - (right_bound - (n - 1))
        #         for i in range(overlap):
        #             if str1[overlap - i - 1] != str2[-(i + 1)]:
        #                 break
        #         else:
        #             if overlap > largest_overlap:
        #                 largest_overlap = overlap
        #                 shortest_superseq = str2 + str1[overlap:] 

        # return shortest_superseq

        m = len(str1)
        n = len(str2)

        memo = {}
        def get_longest_shared_subseq(i, j):
            if i == len(str1) or j == len(str2):
                return ''
            elif (i, j) in memo:
                return memo[(i, j)]
            else:
                if str1[i] == str2[j]:
                    subseq = str1[i] + get_longest_shared_subseq(i + 1, j + 1)
                else:
                    subseq = max(get_longest_shared_subseq(i + 1, j),
                                 get_longest_shared_subseq(i, j + 1),
                                 key=len)
                memo[(i, j)] = subseq
                return subseq

        subseq = get_longest_shared_subseq(0, 0)
        print(subseq)

        str1_additions = defaultdict(list)
        j = 0
        for i in range(m):
            if j < len(subseq) and str1[i] == subseq[j]:
                j += 1
            else:
                str1_additions[j].append(str1[i])
        
        str2_additions = defaultdict(list)
        j = 0
        for i in range(n):
            if j < len(subseq) and str2[i] == subseq[j]:
                j += 1
            else:
                str2_additions[j].append(str2[i])

        print(str1_additions)
        print(str2_additions)

        s = []
        for i in range(len(subseq)):
            for c in str1_additions[i]:
                s.append(c)
            for c in str2_additions[i]:
                s.append(c) 
            s.append(subseq[i])

        for c in str1_additions[len(subseq)]:
            s.append(c)
        for c in str2_additions[len(subseq)]:
            s.append(c) 

        return ''.join(s)


        
152159