<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 {
public:
    int n;
    vector<vector<int>> overlap;  // Overlap table
    vector<vector<string>> dp;    // DP table to store intermediate superstrings

    // Function to calculate the overlap between two strings
    int calculate_overlap(const string& a, const string& b) {
        int max_overlap = 0;
        for (int i = 1; i <= min(a.size(), b.size()); ++i) {
            if (a.substr(a.size() - i) == b.substr(0, i)) {
                max_overlap = i;
            }
        }
        return max_overlap;
    }

    // Recursive DP function to find the shortest superstring
    string f(int mask, int last, vector<string>& words) {
        if (mask == (1 << n) - 1)
            return "";  // All words have been used, return empty string

        if (!dp[mask][last].empty())
            return dp[mask][last];  // Return cached result

        string ans = "";  // Initialize with a large lexicographical string
        for (int i = 0; i < n; i++) {
            if (!(mask & (1 << i))) {  // If the i-th word is not yet used
                string candidate = words[i].substr(overlap[last][i]) + f(mask | (1 << i), i, words);
                if (ans.size() == 0 || candidate.size() < ans.size()) {
                    ans = candidate;
                }
            }
        }

        return dp[mask][last] = ans;  // Store and return the result
    }

    string shortestSuperstring(vector<string>& words) {
        n = words.size();
        
        // Initialize overlap table and dp table
        overlap = vector<vector<int>>(n, vector<int>(n, 0));
        dp = vector<vector<string>>((1 << n), vector<string>(n, ""));

        // Calculate the overlap between every pair of words
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (i != j) {
                    overlap[i][j] = calculate_overlap(words[i], words[j]);
                }
            }
        }

        // Try starting with each word
        string ans = "";  // Initialize with a large lexicographical string
        for (int i = 0; i < n; i++) {
            string candidate = words[i] + f(1 << i, i, words);
            if (ans.size() == 0 || candidate.size() < ans.size()) {
                ans = candidate;
            }
        }

        return ans;
    }
};

469120