-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30.cpp
More file actions
47 lines (46 loc) · 1.45 KB
/
30.cpp
File metadata and controls
47 lines (46 loc) · 1.45 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
class Solution {
public:
bool check(unordered_map<string,int>& ump, unordered_map<string,int>& tmp){
for(auto& [w,val]:ump){
if(!tmp.count(w) || tmp[w]!=val)
return false;
}
return true;
}
vector<int> findSubstring(string s, vector<string>& words) {
int number_of_words = words.size();
if(number_of_words==0)
return {};
int wordsize = words[0].size();
if(wordsize==0)
return {};
int total_size_of_word = wordsize*number_of_words;
int string_size = s.size();
if(total_size_of_word>string_size)
return {};
unordered_map<string,int> ump,ump2;
for(auto& word: words)
ump[word]++;
vector<int> res;
for(int i=0; i<string_size-total_size_of_word+1; i++){
string tmp = s.substr(i,total_size_of_word);
bool possible = true;
for(int j=0; j<total_size_of_word-wordsize+1;j+=wordsize){
string word = tmp.substr(j,wordsize);
if(!ump.count(word)){
possible = false;
break;
}
if(!ump2.count(word))
ump2[word] = 0;
ump2[word]++;
}
if(possible){
if(check(ump,ump2))
res.push_back(i);
}
ump2.clear();
}
return res;
}
};