<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 {
private:
    int n, finalMask;
    int best;
    int dp[12][1 << 12];
    int nextt[12][1 << 12];
    // to find max number of common character in two string
    int common(const string& s1 , const string& s2) {
        int i = 1;
        int ans = 0;
        while(i <= s1.size() and i <= s2.size()) {
            if(s1.substr(s1.size() - i , i) == s2.substr(0 , i))
                ans = i;
            i++;
        }
        return ans;
    }
    // main recursive dp function
    int solve(vector<string>& words, int prev, int mask) {
        // base case
        if(mask == finalMask) {
            return 0;
        }
        // memoization case
        if(dp[prev][mask] != -1)    return dp[prev][mask];
        // calling recursion
        int ret = 1e9;
        int next = -1;
        for(int i = 0; i < n; i++) {
            if(!(mask & (1 << i))) {
                int comm = common(words[prev], words[i]);
                int curr = (words[i].size() - comm) + solve(words, i, (mask | (1 << i)));
                if(ret > curr) {
                    ret = curr;
                    next = i;
                }
            }
        }
        nextt[prev][mask] = next;
        return dp[prev][mask] = ret;
    }
public:
    string shortestSuperstring(vector<string>& words) {
        // travelling salesman problem + retracing path using parent array
        n = words.size();
        finalMask = (1 << n) - 1;
        memset(dp, -1, sizeof(dp));
        memset(nextt, -1, sizeof(dp));
        int mn = 1e9;
        int begin = -1;
        for(int i = 0; i < n; i++) {
            int curr = words[i].size() + solve(words, i, (1 << i));
            if(mn > curr) {
                mn = curr;
                begin = i;
            }
        }
        // now, retracing the answer
        string ret = "";
        int mask = 0;
        while(begin != -1) {
            string prev = words[begin];
            mask |= (1 << begin);
            begin = nextt[begin][mask];
            string next = begin == -1 ? "" : words[begin];
            int comm = common(prev, next);
            ret += prev.substr(0, prev.size() - comm);
        }
        return ret;
    }
};
99520