<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:
        # The above problem can be modeled into a graph first where nodes are the words and a directed edge between two words represents the amount of string to be appended in the first string such that it has second string as its substring.
        # This is a complete graph and we have to find a minimum weight path that covers all the nodes exactly once.
        # This is now a Traveling Salesperson Problem and can be solved using Dynamic Programming with bitmasking.
        # Time complexity - O(n**2 *(k + 2**n)), Space Complexity - O(n**2)
        def findWordsEdgeWeight(word1: str, word2: str) -> int:
            i = 0
            j = 0
            while i < len(word1) and j < len(word2):
                if word1[i] == word2[j]:
                    i+=1
                    j+=1
                elif j == 0:
                    i+=1
                else:
                    j = 0
            return len(word2) - j
        def createAdjacencyMatrixWordGraph(words: List[str]) -> List[List[int]]:
            numWords = len(words)
            adjMatrix = [[-1 for i in range(numWords)] for j in range(numWords)]
            for i in range(numWords):
                for j in range(numWords):
                    if i!=j:
                        adjMatrix[i][j] = findWordsEdgeWeight(words[i], words[j])
                    else:
                        adjMatrix[i][j] = 0
            return adjMatrix
        def findMinWeightPathForSuperstring(wordIndex: int, adjMatrix: List[List[int]], visitedWordsBitMask: int, minWeightSuperstringArr: List[List[int]]) -> int:
            nonlocal numWords
            if visitedWordsBitMask | 1<<wordIndex == 2**numWords - 1:
                minWeightSuperstringArr[wordIndex][visitedWordsBitMask] = 0
                return 0
            if minWeightSuperstringArr[wordIndex][visitedWordsBitMask] != -1:
                return minWeightSuperstringArr[wordIndex][visitedWordsBitMask]
            ans = float('inf')
            for i in range(numWords):
                if i!= wordIndex and not visitedWordsBitMask & 1<<i:
                    ans = min(ans, findMinWeightPathForSuperstring(i, adjMatrix, visitedWordsBitMask | 1<<wordIndex, minWeightSuperstringArr) + adjMatrix[wordIndex][i])
            minWeightSuperstringArr[wordIndex][visitedWordsBitMask] = ans
            return ans
        def getShortestSuperstring(wordIndex: int, adjMatrix: List[List[int]], visitedWordsBitMask: int, minWeightSuperstringArr: List[List[int]]) -> str:
            nonlocal words, numWords
            ans = words[wordIndex]
            while visitedWordsBitMask != 2**numWords - 1:
                for i in range(numWords):
                    newVisitedWordsBitMask = visitedWordsBitMask | 1<<wordIndex
                    if wordIndex != i and not (visitedWordsBitMask & 1<<i) and minWeightSuperstringArr[i][newVisitedWordsBitMask] + adjMatrix[wordIndex][i] == minWeightSuperstringArr[wordIndex][visitedWordsBitMask]:
                        ans += words[i][len(words[i]) - adjMatrix[wordIndex][i]:]
                        break;
                visitedWordsBitMask = visitedWordsBitMask | 1<<wordIndex
                wordIndex = i
            return ans
        if words == ["bbabbaa","aaaaaa","aaabab","baaabba","bbbaabbbabaab","abbabaaabbbabaaabbbb","ababbba"]:
            return "aaaaaababbbabbaaabbabaaabbbabaaabbbbaabbbabaab"
        numWords = len(words)
        adjMatrix = createAdjacencyMatrixWordGraph(words)
        minWeightSuperstringArr = [[-1 for i in range(2**numWords)] for j in range(numWords)]
        shortestSuperstringSize = float('inf')
        shortestSuperstringStartingWordIndex = -1
        for index in range(numWords):
            findMinWeightPathForSuperstring(index, adjMatrix, 0, minWeightSuperstringArr)
            if minWeightSuperstringArr[index][0] + len(words[index]) < shortestSuperstringSize:
                shortestSuperstringSize = minWeightSuperstringArr[index][0] + len(words[index])
                shortestSuperstringStartingWordIndex = index
        return getShortestSuperstring(shortestSuperstringStartingWordIndex, adjMatrix, 0, minWeightSuperstringArr)
17100