-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathImplement strStr.cpp
More file actions
42 lines (41 loc) · 1.09 KB
/
Implement strStr.cpp
File metadata and controls
42 lines (41 loc) · 1.09 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
class Solution {
public:
vector<int> makenext(char *s, int n){
vector<int> next;
next.push_back(-1);
for (int i = 1; i<=n; i++){
int j = next[i-1];
while (j>=0 && s[j] != s[i-1]){
j = next[j];
}
next.push_back(j+1);
}
return next;
}
vector<int> match(char *s, int n, char *p, int m, vector<int> &next){
vector<int> res;
int j = 0;
for (int i = 0; i<n; i++){
while (j>=0 && s[i] != p[j]){
j = next[j];
}
j++;
if (j>=m){
res.push_back(i-m+1);
j = next[j];
}
}
return res;
}
char *strStr(char *haystack, char *needle) {
int m = strlen(needle);
if (m == 0) return haystack;
vector<int> next = makenext(needle, m);
vector<int> res = match(haystack, strlen(haystack), needle, m, next);
if (res.size()>0){
return haystack + res[0];
}else{
return NULL;
}
}
};