forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0030.cpp
More file actions
43 lines (39 loc) · 1.34 KB
/
0030.cpp
File metadata and controls
43 lines (39 loc) · 1.34 KB
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
39
40
41
42
43
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
if (s.empty() || words.empty()) return {};
vector<int> ans;
unordered_map<string, int> map;
for (const auto& word : words) map[word]++;
const int m = s.size();
const int wordLen = words[0].size();
for (int i = 0; i < wordLen; i++) {
int index = i;
int count = 0;
unordered_map<string, int> tempMap;
for (int j = i; j <= m - wordLen; j += wordLen) {
string str = s.substr(j, wordLen);
if (map.count(str)) {
tempMap[str]++;
count++;
while (tempMap[str] > map[str]) {
tempMap[s.substr(index, wordLen)]--;
count--;
index += wordLen;
}
if (count == words.size()) {
ans.push_back(index);
tempMap[s.substr(index, wordLen)]--;
count--;
index += wordLen;
}
} else {
tempMap.clear();
count = 0;
index = j + wordLen;
}
}
}
return ans;
}
};