-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path30.cc
55 lines (51 loc) · 1.76 KB
/
30.cc
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
54
55
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> res;
int wordNum = words.size();
int wordLen = words[0].size();
int windowLen = wordLen * wordNum;
int sLen = s.size();
map<string, int> Map;
for (int i = 0; i < wordNum; ++i) {
++Map[words[i]];
}
for (int i = 0; i < wordLen; ++i) {
int count = 0;
map<string, int> curMap;
int left = i;
for (int j = left; (j + wordLen <= sLen) && (left + windowLen <= sLen); j += wordLen) {
string word = s.substr(j, wordLen);
if (Map[word] == 0) {
count = 0;
curMap.clear();
left = j + wordLen;
} else {
++curMap[word];
if (curMap[word] <= Map[word]) {
++count;
if (count == wordNum) {
res.push_back(left);
string tmp = s.substr(left, wordLen);
--curMap[tmp];
left += wordLen;
--count;
}
} else {
for (;;) {
string tmp = s.substr(left, wordLen);
left += wordLen;
--curMap[tmp];
if (tmp == word) {
break;
} else {
--count;
}
}
}
}
}
}
return res;
}
};