<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:
  std::vector<int> findSubstring(std::string s, std::vector<std::string> &words) {
    std::vector<int> substring_indices;
    // within the sliding window of length (n * l), maintain unused_words
    //
    std::unordered_map<std::string, int> window_words; // window_words inside the sliding_widow;
    std::unordered_map<std::string, int> target;
    std::unordered_set<std::string> diff; // diff = target - window_words;
    int m = s.size();
    int n = words.size();
    int l = words.front().size();
    int sliding_window_len = n * l;

    for (const std::string &word : words) {
      target[word] += 1;
    }

    for (int offset = 0; offset < l; offset++) {
      if (offset + sliding_window_len > m) {
        break;
      }

      window_words.clear();
      diff.clear();

      // compute window_words against the first sliding window
      int start_idx = offset;
      for (int i = 0; i < n; i++) {
        const std::string &word = s.substr(offset + i * l, l);
        window_words[word] += 1;
      }

      for (auto iter : target) {
        auto window_iter = window_words.find(iter.first);
        if (window_iter == window_words.end() || window_iter->second != iter.second) {
          diff.insert(iter.first);
        }
      }

      if (diff.empty()) {
        substring_indices.push_back(start_idx);
      }

      // sliding the window by l, until the end of s
      // if unused_words.empty() == true, record the index;
      while (start_idx + sliding_window_len + l <= m) {
        const std::string &discarded_word = s.substr(start_idx, l);
        const std::string &new_word = s.substr(start_idx + sliding_window_len, l);
        start_idx += l;

        auto discarded_window_iter = window_words.find(discarded_word);
        discarded_window_iter->second -= 1;

        window_words[new_word] += 1;
        auto new_word_iter = window_words.find(new_word);

        for (auto window_iter : {discarded_window_iter, new_word_iter}) {
          auto target_iter = target.find(window_iter->first);
          if (target_iter != target.end()) {
            if (window_iter->second == target_iter->second) {
              diff.erase(window_iter->first);
            } else {
              diff.insert(window_iter->first);
            }
          }
        }

        if (discarded_window_iter->second == 0) {
            window_words.erase(discarded_window_iter);
        }

        if (diff.empty()) {
          substring_indices.push_back(start_idx);
        }
      }
    }

    return substring_indices;
  }
};
44337