<p>Given an array of strings <code>words</code>, return <em>the smallest string that contains each string in</em> <code>words</code> <em>as a substring</em>. If there are multiple valid strings of the smallest length, return <strong>any of them</strong>.</p>

<p>You may assume that no string in <code>words</code> is a substring of another string in <code>words</code>.</p>

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

<pre>
<strong>Input:</strong> words = [&quot;alex&quot;,&quot;loves&quot;,&quot;leetcode&quot;]
<strong>Output:</strong> &quot;alexlovesleetcode&quot;
<strong>Explanation:</strong> All permutations of &quot;alex&quot;,&quot;loves&quot;,&quot;leetcode&quot; would also be accepted.
</pre>

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

<pre>
<strong>Input:</strong> words = [&quot;catg&quot;,&quot;ctaagt&quot;,&quot;gcta&quot;,&quot;ttca&quot;,&quot;atgcatc&quot;]
<strong>Output:</strong> &quot;gctaagttcatgcatc&quot;
</pre>

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

<ul>
	<li><code>1 &lt;= words.length &lt;= 12</code></li>
	<li><code>1 &lt;= words[i].length &lt;= 20</code></li>
	<li><code>words[i]</code> consists of lowercase English letters.</li>
	<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
</ul>

class Solution:
    def shortestSuperstring(self, words: List[str]) -> str:
        from itertools import permutations
        
        def overlap(a, b):
            max_overlap = min(len(a), len(b))
            for i in range(max_overlap, 0, -1):
                if a[-i:] == b[:i]:
                    return i
            return 0
        
        def get_superstring(words):
            n = len(words)
            overlaps = [[overlap(words[i], words[j]) for j in range(n)] for i in range(n)]
            dp = [[(0, []) for _ in range(1 << n)] for _ in range(n)]
            for mask in range(1 << n):
                for u in range(n):
                    if mask & (1 << u):
                        for v in range(n):
                            if not mask & (1 << v):
                                new_mask = mask | (1 << v)
                                new_val = dp[u][mask][0] + overlaps[u][v]
                                if new_val > dp[v][new_mask][0]:
                                    dp[v][new_mask] = (new_val, dp[u][mask][1] + [u])
        
            max_overlap = 0
            path = []
            for u in range(n):
                if dp[u][(1 << n) - 1][0] > max_overlap:
                    max_overlap = dp[u][(1 << n) - 1][0]
                    path = dp[u][(1 << n) - 1][1] + [u]
        
            used = [False] * n
            for u in path:
                used[u] = True
            for i in range(n):
                if not used[i]:
                    path.append(i)
        
            result = words[path[0]]
            for i in range(1, len(path)):
                u = path[i-1]
                v = path[i]
                result += words[v][overlaps[u][v]:]
            return result
        
        return get_superstring(words)
24100