<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 TrieNode{
    public:
    int count;
    char val;
    TrieNode* children[26];
    bool end;
    int tmpCount;
    TrieNode(){
        val = '.';
        for(int i =0; i < 26; ++i) children[i] = nullptr;
        end = false;
        count = 0;
        tmpCount = 0;
    }
    TrieNode(char _val){
        val = _val;
        for(int i =0; i < 26; ++i) children[i] = nullptr;
        end = false;
        count = 0;
        tmpCount = 0;
    }
};

class Trie{
    TrieNode* root;
    public:
    Trie(){
        root = new TrieNode();
    }
    Trie(Trie& other){
        root = new TrieNode();
        stack<TrieNode*> st;
        stack<TrieNode*> stC;
        stC.push(other.root);
        st.push(root);
        while(!stC.empty()){
            TrieNode* curr = st.top();
            TrieNode* oth = stC.top();
            //copy the parameters
            curr -> val = oth -> val;
            curr -> end = oth -> end;
            curr -> count = oth -> count;
            st.pop();
            stC.pop();
            for(int i =0 ; i < 26; ++i){
                if(oth -> children[i]){
                    curr -> children[i] = new TrieNode((char) (i + 'a'));
                    st.push(curr -> children[i]);
                    stC.push(oth -> children[i]);
                }
            }
        }
    }
    void insert(string word){
        TrieNode* curr = root;
        for(char c : word){
            if(!curr -> children[c - 'a']){
                curr -> children[c - 'a'] = new TrieNode(c);
            }
            curr = curr -> children[c - 'a'];
        }
        curr -> end = true;
        curr -> count++;
    }
    bool jCheck(string word){
        TrieNode* curr = root;
        for(char c : word){
            if(!curr -> children[c - 'a']){
                return false;
            }
            curr = curr -> children[c - 'a'];
        }
        return true;
    }
    bool present(string word){
        TrieNode* curr = root;
        for(char c : word){
            if(!curr -> children[c - 'a']){
                return false;
            }
            curr = curr -> children[c - 'a'];
        }
        bool ret = curr -> count > 0;
        if(ret){
            curr -> count--;
            return true;
        }
        return false;
    }
    void refresh(){
        stack<TrieNode*> st;
        st.push(root);
        while(!st.empty()){
            TrieNode* curr = st.top();
            st.pop();
            curr -> tmpCount = curr -> count;
            for(int i =0 ;i < 26; ++i){
                if(curr -> children[i]){
                    st.push(curr -> children[i]);
                }
            }
        }
    }
    bool getCount(string word){
        TrieNode* curr = root;
        for(char c : word){
            if(!curr -> children[c - 'a']){
                return 0;
            }
            curr = curr -> children[c - 'a'];
        }
        if(curr -> tmpCount <= 0) return 0;
        curr -> tmpCount--;
        return true;
    }
};

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> ans;
        int len = s.length();
        int n = words.size();
        int w = words[0].size();
        Trie myT = Trie();
        for(auto word : words) myT.insert(word);
        myT.refresh();//assign tmpCount
        //cout << "check " << myT.getCount("foo") << '\n';
        int l =0, r = w-1;
        while(r < len){
            if(myT.jCheck(s.substr(l, r-l+1))){
                //check if it forms
                int tl = l, tr = r;
                int cnt = 0;
                bool flag = true;
                while(cnt < n){
                    if(!myT.getCount(s.substr(tl, tr-tl+1))){
                        cout << "due to 1 - "<< l << '\n';
                        flag = false;
                        break;
                    }
                    cnt++;
                    tl = tr+1, tr = tl + w-1;
                    if(cnt < n && tr >= len){
                        cout << "due to 2 - "<< l << '\n';
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    ans.push_back(l);
                }
                myT.refresh();
            }
            l++, r++;//slide the window
        }
        //cout << "check " << myT.getCount("foo");
        return ans;
    }
};
76137