<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> concatIndices;
        bool sameString = true;

        string accumString = words[0];
        for(int i = 1; i < words.size(); i++){
            if(words[i] != words[i-1]){
                sameString = false;
            }
            accumString += words[i];
        }

        if(accumString.size() > s.size()){
            return concatIndices;
        }

        if(accumString.size() == words.size() * words[0].size() and sameString){
            for(int i = 0; i < s.size()-accumString.size()+1; i++){
                if(s.substr(i, accumString.size()) == accumString){
                    concatIndices.push_back(i);
                }
            }
            return concatIndices;
        }
        

        unordered_multiset<string> uset;
        uset.reserve(words.size());
        uset.insert(words.begin(), words.end());

        queue<string> queueBackup;

        int startWordIndex = 0;
        int endWordIndex = words.size() * words[0].size();
        bool foundWord = false;
        string firstWord = "";

        for(int i = 0; endWordIndex <= s.size();i++){
            // cout << "i: " << i << endl;
            // cout << "first word: " << firstWord << endl;
            // cout << "size of set: " << uset.size() << endl;
            // cout << "size of queue: " << queueBackup.size() << endl;
            if(i >= endWordIndex and !foundWord){
                dsReset(uset, queueBackup);
                firstWord = "";
                endWordIndex++;
                i = startWordIndex;
                startWordIndex++;
                foundWord = false;
                continue;
            }
            else if(i >= endWordIndex and foundWord){
                concatIndices.push_back(startWordIndex);
                firstWord = getWordFromQueue(uset, queueBackup);
                endWordIndex += words[0].size();
                startWordIndex += words[0].size();
                i = endWordIndex-words[0].size()-1;
                foundWord = false;
                continue;
            }

            string nextWord = s.substr(i, words[0].size());

            auto it = uset.find(nextWord);

            if(!firstWord.empty()){
                if(firstWord == nextWord and it != uset.end()){
                    concatIndices.push_back(startWordIndex);
                    startWordIndex += words[0].size();
                    endWordIndex += words[0].size();
                    i = endWordIndex-words[0].size()-1;
                    uset.erase(it);
                    queueBackup.push(nextWord);
                    uset.insert(queueBackup.front());
                    firstWord = queueBackup.front();
                    continue;
                }
                else{
                    dsReset(uset, queueBackup);
                    i = startWordIndex - 1;
                    firstWord = "";
                    continue;
                }
            }

            if(it == uset.end()){
                dsReset(uset, queueBackup);
                firstWord = "";
                foundWord = false;
                endWordIndex++;
                i = startWordIndex;
                startWordIndex++;
                continue;
            }
            else{
                queueBackup.push(*it);
                uset.erase(it);
                foundWord = true;
                i += words[0].size()-1;
            }
        }

        return concatIndices;
    }

    void dsReset(unordered_multiset<string>& umset, queue<string>& queueBackup){
        while(queueBackup.size() > 0){
            umset.insert(queueBackup.front());
            queueBackup.pop();
        }
    }

    string getWordFromQueue(unordered_multiset<string>& umset, queue<string>& queueBackup){
        string firstWord = "";
        if(queueBackup.size() > 0){
            firstWord = queueBackup.front();
            umset.insert(queueBackup.front());
            queueBackup.pop();
        }
        return firstWord;
    }
};
215262