<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:

    string solve(int idx , int vis , vector<string>&a , vector<vector<int>> &w ,vector<vector<string>> &dp){
        int  n = a.size();
        if(vis == ((1<<n)-1))
            return "";
        if(!dp[idx][vis].empty())
            return dp[idx][vis];

        int mini = 1e9;
        string minS,tmp;
        for(int i=0;i<n;i++){
            if((vis & (1<<i))==0){
                tmp = a[i].substr(w[idx][i]) + solve(i, (vis | (1<<i)) , a,w,dp);
                if(tmp.size()<mini){
                    mini = tmp.size();
                    minS=tmp;
                }
            }
        }
        return dp[idx][vis] = minS;
    }
    
    string shortestSuperstring(vector<string>& a) {
        int N = a.size();
        vector<vector<string>> dp(N+1 , vector<string>((1<<N)+1,""));
        vector<vector<int>> w(N,vector<int>(N,0));
        //O(N^2 * W)
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                if(i==j)
                    w[i][j]=a[i].size();
                else{
                    int k = min(a[i].size(),a[j].size());
                    // max overlap value when taking i from end and j from start
                    for(;k>=0;k--){
                        if(a[i].substr(a[i].size()-k) == a[j].substr(0,k)){
                            w[i][j]=k;
                            break;
                        }
                    }
                }
            }
        }
        // for(vector<int> r : w){
        //     for(int i : r) cout<<i<<" ";
        //     cout<<endl;
        // }
        string minS,tmp;
        int mini=1e9;
        for(int idx =0 ;idx<N;idx++){
            int vis = 0;
            vis = vis | (1<<idx);
            tmp = a[idx] + solve(idx,vis,a,w,dp);
            if(tmp.size()<mini){
                mini = tmp.size();
                minS=tmp;
            }
        }
        return minS;        
    }
};
413680