<p>You are given a string <code>s</code> and an array of strings <code>words</code>. All the strings of <code>words</code> are of <strong>the same length</strong>.</p>

<p>A <strong>concatenated string</strong> is a string that exactly contains all the strings of any permutation of <code>words</code> concatenated.</p>

<ul>
	<li>For example, if <code>words = [&quot;ab&quot;,&quot;cd&quot;,&quot;ef&quot;]</code>, then <code>&quot;abcdef&quot;</code>, <code>&quot;abefcd&quot;</code>, <code>&quot;cdabef&quot;</code>, <code>&quot;cdefab&quot;</code>, <code>&quot;efabcd&quot;</code>, and <code>&quot;efcdab&quot;</code> are all concatenated strings. <code>&quot;acdbef&quot;</code> is not a concatenated string because it is not the concatenation of any permutation of <code>words</code>.</li>
</ul>

<p>Return an array of <em>the starting indices</em> of all the concatenated substrings in <code>s</code>. You can return the answer in <strong>any order</strong>.</p>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;barfoothefoobarman&quot;, words = [&quot;foo&quot;,&quot;bar&quot;]</span></p>

<p><strong>Output:</strong> <span class="example-io">[0,9]</span></p>

<p><strong>Explanation:</strong></p>

<p>The substring starting at 0 is <code>&quot;barfoo&quot;</code>. It is the concatenation of <code>[&quot;bar&quot;,&quot;foo&quot;]</code> which is a permutation of <code>words</code>.<br />
The substring starting at 9 is <code>&quot;foobar&quot;</code>. It is the concatenation of <code>[&quot;foo&quot;,&quot;bar&quot;]</code> which is a permutation of <code>words</code>.</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;wordgoodgoodgoodbestword&quot;, words = [&quot;word&quot;,&quot;good&quot;,&quot;best&quot;,&quot;word&quot;]</span></p>

<p><strong>Output:</strong> <span class="example-io">[]</span></p>

<p><strong>Explanation:</strong></p>

<p>There is no concatenated substring.</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;barfoofoobarthefoobarman&quot;, words = [&quot;bar&quot;,&quot;foo&quot;,&quot;the&quot;]</span></p>

<p><strong>Output:</strong> <span class="example-io">[6,9,12]</span></p>

<p><strong>Explanation:</strong></p>

<p>The substring starting at 6 is <code>&quot;foobarthe&quot;</code>. It is the concatenation of <code>[&quot;foo&quot;,&quot;bar&quot;,&quot;the&quot;]</code>.<br />
The substring starting at 9 is <code>&quot;barthefoo&quot;</code>. It is the concatenation of <code>[&quot;bar&quot;,&quot;the&quot;,&quot;foo&quot;]</code>.<br />
The substring starting at 12 is <code>&quot;thefoobar&quot;</code>. It is the concatenation of <code>[&quot;the&quot;,&quot;foo&quot;,&quot;bar&quot;]</code>.</p>
</div>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= words.length &lt;= 5000</code></li>
	<li><code>1 &lt;= words[i].length &lt;= 30</code></li>
	<li><code>s</code> and <code>words[i]</code> consist of lowercase English letters.</li>
</ul>

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> ret;
        int sLen = s.size();

        unordered_map<string, int> wordCnt;
        int totalLen = 0;
        for (int i = 0; i < words.size(); ++i) {
            ++wordCnt[words[i]];
            totalLen += words[i].size();
        }

        if (wordCnt.size() == 1) {
            const string& tmpWord = wordCnt.begin()->first;
            int tmpLen = tmpWord.size();
            int tmpCnt = wordCnt.begin()->second;
            string str;
            str.resize(tmpWord.size() * tmpCnt);
            for (int i = 0; i < tmpCnt; ++i) {
                for (int j = 0; j < tmpLen; ++j) {
                    str[i * tmpLen + j] = tmpWord[j];
                }
            }
            wordCnt.clear();
            wordCnt[str] = 1;
        }

        if (sLen < totalLen) return ret;

        int chCnt[26];
        memset(chCnt, 0, sizeof(chCnt));
        vector<string> wordsIdx;
        for (auto kv : wordCnt) {
            //printf("%s:%d\n", kv.first.c_str(), kv.second);
            string word = kv.first;
            wordsIdx.push_back(word);
            int len = word.size();
            for (int i = 0; i < len; ++i) {
                chCnt[word[i] - 'a'] += kv.second;
            }
        }
        sort(wordsIdx.begin(), wordsIdx.end());

        for (int i = 0; i < totalLen; ++i) {
            --chCnt[s[i] - 'a'];
        }
        int noneZero = 0;
        for (int i = 0; i < 26; ++i) {
            noneZero += ((chCnt[i] != 0) ? 1 : 0);
        }
        //printf("zero:%d\n", noneZero);

        int trackStack[5010][2];
        int stackSZ = 0;
        for (int i = 0; i + totalLen <= sLen; ++i) {
            if (i > 0) {
                int begIdx = s[i - 1] - 'a';
                int endIdx = s[i - 1 + totalLen] - 'a';
                if (begIdx != endIdx) {
                    if (chCnt[begIdx] == -1) {
                        --noneZero;
                    } else if (chCnt[begIdx] == 0) {
                        ++noneZero;
                    }
                }
                ++chCnt[begIdx];
                if (begIdx != endIdx) {
                    if (chCnt[endIdx] == 1) {
                        --noneZero;
                    } else if (chCnt[endIdx] == 0) {
                        ++noneZero;
                    }
                }
                --chCnt[endIdx];
            }
            /*
            printf("i:%d zero:%d\n", i, noneZero);
            for (int j = 0; j < 26; ++j) {
                if (chCnt[j] != 0) {
                    printf("%c:%d\n", 'a' + j, chCnt[j]);
                }
            }
            */
            if (noneZero <= 0) {
                //printf("%d\n", i);

                trackStack[0][0] = i;
                trackStack[0][1] = -1;
                stackSZ = 1;

                while (stackSZ > 0) {
                    --stackSZ;
                    int sI = trackStack[stackSZ][0];
                    int nwI = trackStack[stackSZ][1];

                    if ((i + totalLen) == sI) {
                        ret.push_back(i);
                        break;
                    }
                    bool found = false;
                    for (int j = nwI + 1; j < wordsIdx.size(); ++j) {
                        const string tmpWord = wordsIdx[j];
                        if (wordCnt[tmpWord] > 0) {
                            bool match = true;
                            int twLen = tmpWord.size();
                            for (int k = 0; match && (k < twLen); ++k) {
                                match = (s[sI + k] == tmpWord[k]);
                            }
                            if (match) {
                                --wordCnt[tmpWord];
                                trackStack[stackSZ][0] = sI;
                                trackStack[stackSZ][1] = j;
                                ++stackSZ;
                                trackStack[stackSZ][0] = sI + twLen;
                                trackStack[stackSZ][1] = -1;
                                ++stackSZ;
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!found) {
                        if (nwI >= 0) {
                            ++wordCnt[wordsIdx[nwI]];
                        }
                    }
                }
                while (stackSZ > 0) {
                    --stackSZ;
                    int nwI = trackStack[stackSZ][1];
                    if (nwI >= 0) {
                        ++wordCnt[wordsIdx[nwI]];
                    }
                }
            }
        }

        return ret;
    }
};
92037