<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:
        # idea: compute "compatibility" between two words, max size L such that the L last chars of s1 are the L first chars of s2
        # then find path s1 -> ... -> sn to maximize compatibility (use dp with bitmasks, states = 2**12 * 12)
        self.words = words
        self.n = len(words)
        self.compats = [[self.compat(i1, i2) if i1 != self.n else 0 for i2 in range(self.n)] for i1 in range(self.n+1)]
        # self.compats[self.n][x] == 0 for all indices x, for convenience.
        self.cache = [[None for _ in range(self.n+1)] for _ in range(2**self.n)]
        _, order = self.rec(0, self.n)
        result = words[order[0]]
        for i in range(1, self.n):
            prefixLength = self.compats[order[i-1]][order[i]]
            result += words[order[i]][prefixLength:]
        return result
    def rec(self, bitmask: int, lastPicked: int) -> Tuple[int, List[int]]:
        # returns (optimal score, order of indices to reach optimal score)
        # lastPicked == n means nothing was picked yet.
        if self.cache[bitmask][lastPicked] is None:
            value = (0, [])
            for i in range(self.n):
                if bitmask & (1 << i) == 0:
                    # the i-th word is available
                    recScore, recOrder = self.rec(bitmask | (1 << i), i)
                    score = recScore + self.compats[lastPicked][i]
                    if score > value[0] or value[0] == 0:
                        value = (score, [i] + recOrder)
            self.cache[bitmask][lastPicked] = value
        return self.cache[bitmask][lastPicked]
    def compat(self, i1: int, i2: int) -> int:
        for prefixLength in range(min(len(self.words[i1]), len(self.words[i2])), -1, -1):
            if self.words[i1][-prefixLength:] == self.words[i2][:prefixLength]:
                return prefixLength
        return 0
21800