-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindSubstring.cpp
More file actions
34 lines (29 loc) · 961 Bytes
/
findSubstring.cpp
File metadata and controls
34 lines (29 loc) · 961 Bytes
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
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
unordered_map<string, int> count;
for(string str:words){
count[str]++;
}
int len = s.length();
int num = words.size();
int wordLen = words[0].size();
vector<int> result;
for(int i =0; i< len - num*wordLen+1; i++){
unordered_map<string, int> seenWords;
int j =0;
for( ; j<num; j++ ){
string word = s.substr(i+j*wordLen, wordLen);
if(count.find(word)!=count.end()){
seenWords[word]++;
if(seenWords[word]>count[word])
break;
}
else
break;
}
if(j == num) result.push_back(i);
}
return result;
}
};