-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindSubstring.cpp
More file actions
53 lines (50 loc) · 1.91 KB
/
findSubstring.cpp
File metadata and controls
53 lines (50 loc) · 1.91 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
44
45
46
47
48
49
50
51
52
53
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
int wordNum = words.size(), wordLen = words[0].size(), len = s.size();
unordered_map<string, int> m;
vector<int> ans;
for (auto &w: words) ++m[w];
for (int i = 0; i < wordLen; ++i) {
unordered_map<string, int> m2;
int num = 0;
for (int j = i; j < len - wordLen * wordNum + 1; j += wordLen) {
bool hasremoved = false;
while (num < wordNum) {
string word = s.substr(j + num * wordLen, wordLen);
if (m.find(word) != m.end()) {
++m2[word];
if (m2[word] > m[word]) {
hasremoved = true;
int removeNum = 0;
while (m2[word] > m[word]) {
string firstWord = s.substr(j + removeNum * wordLen, wordLen);
--m2[firstWord];
++removeNum;
}
num = num - removeNum + 1;
j += (removeNum - 1) * wordLen;
break;
}
}
else {
m2.clear();
j += num * wordLen;
num = 0;
break;
}
++num;
}
if (num == wordNum)
ans.push_back(j);
if (num > 0 && !hasremoved) {
string firstWord = s.substr(j, wordLen);
--m2[firstWord];
--num;
}
}
}
return ans;
}
};
// https://leetcode.wang/leetCode-30-Substring-with-Concatenation-of-All-Words.html