<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 merge(vector<string> &arr, int l, int m, int r) {
        int leftArrSize = m - l + 1, rightArrSize = r - m;

        vector<string> leftArr, rightArr;

        for (int i = 0; i < leftArrSize; i++) leftArr.push_back(arr[l+i]);
        for (int i = 0; i < rightArrSize; i++) rightArr.push_back(arr[m+1+i]);

        int i = 0, j = 0, p = l;
        while (i < leftArrSize && j < rightArrSize) arr[p++] = leftArr[i].size() <= rightArr[j].size() ? leftArr[i++] : rightArr[j++];
        while (i < leftArrSize) arr[p++] = leftArr[i++];
        while (j < rightArrSize) arr[p++] = rightArr[j++];
    }
    void mergeSort(vector<string> &words, int l, int r) {
        if (l >= r) return;

        int m = l + ((r - l) / 2);

        mergeSort(words, l, m);
        mergeSort(words, m+1, r);
        merge(words, l, m, r);
    }
    bool isStringMatching(string &w1, string &w2) {
        if (w2.size() < w1.size()) return false;
        for (int i = 0; i <= w2.size() - w1.size(); i++) {
            bool flag = true;
            for (int j = 0; j < w1.size(); j++) {
                if (w1[j] != w2[i+j]) {
                    flag = false;
                    break;
                }
            }
            if (flag) return true;
        }
        return false;
    }
    vector<string> stringMatching(vector<string>& words) {
        vector<string> ans;
        mergeSort(words, 0, words.size()-1);
        for (int i = 0; i < words.size(); i++) {
            for (int j = i+1; j < words.size(); j++) {
                if (isStringMatching(words[i], words[j])) {
                    ans.push_back(words[i]);
                    break;
                }
            }
        }
        return ans;
    }
};
15400