-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementStrStr.cc
More file actions
59 lines (53 loc) · 1.79 KB
/
Copy pathImplementStrStr.cc
File metadata and controls
59 lines (53 loc) · 1.79 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
namespace ImplementStrStr {
// KMP solution, complexity is O(m + n)
class Solution {
public:
// generate "next" pattern data
void getNext(const char* pattern, int * next){
next[0] = -1;
int k = -1, j = 0;
while(pattern[j] != '\0') {
if(k!= -1 && pattern[k] != pattern[j] )
k = next[k];
++j; ++k;
if(pattern[k]== pattern[j])
next[j] = next[k];
else
next[j] = k;
}
}
char * strStr(char *haystack, char *needle) {
if( !haystack || !needle)
return NULL;
// get needle's length
int len = 0;
const char * c = needle;
while(*c++ != '\0') len++;
// get next pattern
int *next = new int[len + 1];
getNext(needle, next);
int matchStart = 0; // the start of match
int i = 0; // the current match pos in haystack
int j = 0; // the current match pos in needle
while(haystack[i] != '\0' && needle[j] != '\0') {
if(haystack[i] == needle[j]) {
++i; ++j;
} else {
// adjust matchStart, i and j according to "next"
matchStart += j - next[j];
if(next[j] != -1) {
j = next[j];
} else {
j = 0;
++i;
}
}
} //while
delete []next;
if(needle[j] == '\0')
return (haystack + matchStart); // matched
else
return NULL;
}
};
}