forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
38 lines (38 loc) · 1.06 KB
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
unordered_map<string, int> cnt;
for (auto& w : words) {
++cnt[w];
}
int m = s.size(), n = words.size(), k = words[0].size();
vector<int> ans;
for (int i = 0; i < k; ++i) {
unordered_map<string, int> cnt1;
int l = i, r = i;
int t = 0;
while (r + k <= m) {
string w = s.substr(r, k);
r += k;
if (!cnt.count(w)) {
cnt1.clear();
l = r;
t = 0;
continue;
}
++cnt1[w];
++t;
while (cnt1[w] > cnt[w]) {
string remove = s.substr(l, k);
l += k;
--cnt1[remove];
--t;
}
if (t == n) {
ans.push_back(l);
}
}
}
return ans;
}
};