<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 {
    vector<string>wrds;
    int n;
    int overlaps(string &a,string &b){
        int toBeMinus = 0;
        int r = b.size();
        int l = a.size();
        for(int j =0;j<r;j++){
            if(j+1>l) break;
            int ll = l-1,rr = j;
            while(rr>=0){
                if(a[ll]!=b[rr]){
                    break;
                }
                ll--;
                rr--;
            }
            if(rr<0){
                toBeMinus = max(toBeMinus,j+1);
            }
        }
        return toBeMinus;
        
    }
    pair<int,string> rec(int prev,int mask,vector<vector<pair<int,string>>>&dp,vector<vector<int>>&table){
        if(mask == ((1<<n)-1)) return {0,""};
        if(dp[prev+1][mask].first!=-1) return dp[prev+1][mask];
        int ans = INT_MAX;
        string ansSec ="";
        for(int i =0;i<n;i++){
            if(((1<<i)&mask)==0){
                pair<int,string>ret = rec(i,(mask|(1<<i)),dp,table);
                int temp = wrds[i].size()+ret.first;
                string rets = wrds[i]+ret.second;
                int toBeMinus = 0;
                if(prev!=-1) toBeMinus = table[prev][i];
                int r = wrds[i].size();
                // if(prev!=-1){
                //     int l = wrds[prev].size();
                //     for(int j =0;j<r;j++){
                //         if(j+1>l) break;
                //         int ll = l-1,rr = j;
                //         while(rr>=0){
                //             if(wrds[prev][ll]!=wrds[i][rr]){
                //                 break;
                //             }
                //             ll--;
                //             rr--;
                //         }
                //         if(rr<0){
                //             toBeMinus = max(toBeMinus,j+1);
                //         }
                //     }
                // }
                if(toBeMinus>0){
                    rets = rets.substr(toBeMinus,temp-toBeMinus);
                }
                temp-=toBeMinus;
                if(ans>temp){
                    ans = temp;
                    ansSec = rets;
                }
            }

        }
        return dp[prev+1][mask]={ans,ansSec};
    }
public:
    string shortestSuperstring(vector<string>& words) {
        wrds=words;
        n = wrds.size();
        vector<vector<int>>table(n+1,vector<int>(n+1,0));
        for(int i =0;i<n;i++){
            for(int j=0;j<n;j++){
                table[i][j]=overlaps(words[i],words[j]);
            }
        }
        vector<vector<pair<int,string>>>dp(n+2,vector<pair<int,string>>((1<<n),{-1,""}));
        return rec(-1,0,dp,table).second;
    }
};
241200