30. 串联所有单词的子串

class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> res;
int n = s.size(), m = words.size(), w = words[0].size();
unordered_map<string, int> mp;
for(auto &x : words) mp[x]++;
for(int i = 0; i < w; ++i){
unordered_map<string, int> tmp;
int cnt = 0;
for(int j = i; j + w <= n; j += w){
if(j >= i + m * w){
auto word = s.substr(j - m * w, w);
tmp[word]--;
if(tmp[word] < mp[word]) --cnt;
}
auto word = s.substr(j, w);
tmp[word]++;
if(tmp[word] <= mp[word]) ++cnt;
if(cnt == m) res.push_back(j - (m - 1) * w);
}
}
return res;
}
};