<p>Given an array of string <code>words</code>, return <em>all strings in </em><code>words</code><em> that is a <strong>substring</strong> of another word</em>. You can return the answer in <strong>any order</strong>.</p>

<p>A <strong>substring</strong> is a contiguous sequence of characters within a string</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;mass&quot;,&quot;as&quot;,&quot;hero&quot;,&quot;superhero&quot;]
<strong>Output:</strong> [&quot;as&quot;,&quot;hero&quot;]
<strong>Explanation:</strong> &quot;as&quot; is substring of &quot;mass&quot; and &quot;hero&quot; is substring of &quot;superhero&quot;.
[&quot;hero&quot;,&quot;as&quot;] is also a valid answer.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;leetcode&quot;,&quot;et&quot;,&quot;code&quot;]
<strong>Output:</strong> [&quot;et&quot;,&quot;code&quot;]
<strong>Explanation:</strong> &quot;et&quot;, &quot;code&quot; are substring of &quot;leetcode&quot;.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;blue&quot;,&quot;green&quot;,&quot;bu&quot;]
<strong>Output:</strong> []
<strong>Explanation:</strong> No string of words is substring of another string.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= words.length &lt;= 100</code></li>
	<li><code>1 &lt;= words[i].length &lt;= 30</code></li>
	<li><code>words[i]</code> contains only lowercase English letters.</li>
	<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
</ul>

class Solution {
public:
    void KMPTable(char* pat,int* table){
        int m=strlen(pat);
        int i=1, j=0;
        table[0]=-1;
        while(i<m){
            if(pat[i]!=pat[j]){
                table[i]=j;
                while(j>=0 && table[i]!=table[j]){
                    j=table[j];
                }
            }
            else{
                table[i]=table[j];
            }
            i++;
            j++;
        }
        table[m]=j;
    }
    bool KMP(char* pat,char* seq){
        int n=strlen(seq);
        int m=strlen(pat);
        if(n<=0 || m<=0) return false;
        if(m>n) return false;
        int table[m+1];
        int i=0,j=0;
        KMPTable(pat,table);
        while(i<n){
            if(pat[j]==seq[i]){
                i++;
                j++;
                if(j==m) return true;
            }
            else{
                j=table[j];
                if(j<0){
                    i++;
                    j++;
                }
            }
        }
        return false;
    }
    vector<string> stringMatching(vector<string>& words) {
        vector<string> v;
        for(int i=0;i<words.size();i++){
            for(int j=0;j<i;j++){
                char pat[words[i].length()+1];
                char seq[words[j].length()+1];
                strcpy(pat,words[i].c_str());
                strcpy(seq,words[j].c_str());
                if(KMP(pat,seq)){
                    v.push_back(words[i]);
                }
            }
        }
        for(int i=0;i<words.size();i++){
            for(int j=0;j<i;j++){
                char pat[words[i].length()+1];
                char seq[words[j].length()+1];
                strcpy(pat,words[i].c_str());
                strcpy(seq,words[j].c_str());
                if(KMP(seq,pat)){
                    v.push_back(words[j]);
                }
            }
        }
        unordered_set<string> s;
        for(int i=0;i<v.size();i++){
            s.insert(v[i]);
        }
        vector<string> f;
        for (auto x : s){
            f.push_back(x);
        }
        return f;
    }
};
11200