-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet30.cpp
More file actions
80 lines (58 loc) · 1.64 KB
/
leet30.cpp
File metadata and controls
80 lines (58 loc) · 1.64 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<unordered_map>
using namespace std;
class Solution{
public:
vector<int> findSubstring(string s, vector<string>& words){
vector<int> res;
unordered_map<string, int> Map;
unordered_map<string, int> temp;
int slen = s.size();
int wlen = words.size();
if (slen == 0 || wlen == 0){
return res;
}
int perlen = words[0].size();
if (wlen*perlen > slen){
return res;
}
for (int i=0; i<wlen; ++i){
Map[words[i]]++;
}
for (int i=0; i+perlen*wlen-1<slen; ++i){
int j = i;
temp.clear();
while (j <= i+wlen*perlen - 1){
string cur_sub = s.substr(j, perlen); // current substring
temp[cur_sub]++;
if (Map[cur_sub] < temp[cur_sub]){
break;
}
else{
j += perlen;
}
if (j>i + wlen*perlen - 1){
res.push_back(i);
}
}
}
return res;
}
};
int main(){
string s = "barfoothefoobarman";
//string s = "foothebarbarfooman";
vector<string> words;
words.push_back("bar");
words.push_back("foo");
Solution sol;
vector<int> res = sol.findSubstring(s, words);
for (int i = 0; i<res.size(); ++i){
cout << res[i] << " ";
}
cout << endl;
return 0;
}