-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path28.cpp
44 lines (37 loc) · 1.07 KB
/
28.cpp
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
class Solution {
public:
int strStr(string haystack, string needle) {
//creating prefix table
int needle_len = needle.length();
vector<int> p(needle_len);
p[0] = -1;
int temp = -1;
for (int i = 1; i < needle_len; i++){
while (temp >= 0 && needle[temp+1] != needle[i]){
temp = p[temp];
}
if (needle[temp+1] == needle[i]){
temp += 1;
}
p[i] = temp;
}
int i = 0, j = 0;
int answer = -1;
while (i < haystack.length()){
//cout << i << " " << j << " " << p[j] << endl;
if (haystack[i] == needle[j]){
j += 1;
if (j == needle_len){
answer = i-needle_len+1;
break;
}
i+=1;
}
else if (j==0)i+=1;
else{
j = p[j-1]+1;
}
}
return answer;
}
};