-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#128.cc
More file actions
50 lines (43 loc) · 1.42 KB
/
LeetCode#128.cc
File metadata and controls
50 lines (43 loc) · 1.42 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
class Solution {
public:
vector<int> findSubstring(string S, vector<string> &L) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> ret;
if(L.size()==0) return ret;
map<string,int> need;
for(int i=0;i<L.size();i++)
++need[L[i]];
int N = L.size();
int len = L[0].length();
for(int i=0;i<len;i++){
int begin = i;
int cnt = 0;
map<string,int> sub;
for(int j=begin;j<=(int)S.length()-len;j+=len){
string w = S.substr(j,len);
if(need.find(w)==need.end()){
sub.clear();
cnt=0;
begin = j+len;
}
else{
++sub[w];
if(sub[w]<=need[w]) cnt++;
else{
while(true){
string t = S.substr(begin,len);
--sub[t];
begin += len;
if(sub[t]<need[t]) cnt--;
else break;
}
}
if(cnt==N) ret.push_back(begin);
}
}
}
sort(ret.begin(),ret.end());
return ret;
}
};