-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0030.cpp
More file actions
36 lines (33 loc) · 1.04 KB
/
Copy path0030.cpp
File metadata and controls
36 lines (33 loc) · 1.04 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
static auto _ = []() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
};
class Solution {
static bool check(const unordered_map<string, int> &count, const string &s, int len) {
static unordered_map<string, int> lcount;
lcount.clear();
for (int i = 0; i < s.size(); i += len) {
string word = s.substr(i, len);
const auto it = count.find(word);
if (it == count.end()) return false;
if (++lcount[word] > it->second) return false;
}
return true;
}
public:
vector<int> findSubstring(const string &s, const vector<string> &words) const {
const int n = words.size(), m = words[0].size();
unordered_map<string, int> count;
for (const string &word : words)
count[word]++;
vector<int> res;
for (int i = 0; i + m * n <= s.size(); i++) {
if (check(count, s.substr(i, m * n), m)) {
res.push_back(i);
}
}
return res;
}
};