-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30.cpp
More file actions
51 lines (44 loc) · 1.59 KB
/
30.cpp
File metadata and controls
51 lines (44 loc) · 1.59 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
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
int n = s.length(); int m = words.size(); int w = words[0].length();
if(n < w*m) return vector<int>();
unordered_map<string, int> ws;
vector<int> count(m);
for(int i = 0; i < m; i++){
if(ws.count(words[i])){
count[ws[words[i]]]++;
} else {
ws[words[i]] = i;
count[i] = 1;
}
}
vector<int> sol;
for(int i = 0; i < w; i++){ //start position
vector<int> contains(m);
queue<int> q; //running queue of words currently in this segment
for(int j = i; j <= n-w; j += w){ //start of each word
if(q.size() == m){
int toRem = q.front(); q.pop(); //word to remove
if(toRem != INT_MAX) contains[toRem]--;
}
string word = s.substr(j, w);
int toAdd = INT_MAX; //word to add
if(ws.count(word)) {
toAdd = ws[word];
contains[toAdd]++;
}
q.push(toAdd);
bool works = true;
for(int i = 0; i < m; i++){
if(contains[i] != count[i]){
works = false;
break;
}
}
if(works) sol.push_back(j - w*(m-1));
}
}
return sol;
}
};