1
1
/*
2
- Author: Annie Kim, [email protected]
3
- Date: Apr 9, 2013
4
- Update: Jul 19, 2013 (Refactor)
2
+
3
+ Date: Dec 17, 2014
5
4
Problem: Implement strStr()
6
5
Difficulty: Easy
7
- Source: http ://leetcode.com/onlinejudge#question_28
6
+ Source: https ://oj. leetcode.com/problems/implement-strstr/
8
7
Notes:
9
8
Implement strStr().
10
9
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
11
10
12
- Solution: Check in the haystack one by one. If not equal to needle, reset the pointer.
11
+ Solution: 1. Check in the haystack one by one. If not equal to needle, reset the pointer.(TLE)
12
+ 2. Classice KMP solution.
13
+ 3. Simplified RK Soluiton. Thanks for [wenyuanhust, [email protected] ]
13
14
*/
14
-
15
15
class Solution {
16
16
public:
17
- char *strStr (char *haystack, char *needle) {
18
- while (true )
19
- {
20
- char *h = haystack, *n = needle;
21
- while (*n != ' \0 ' && *h == *n)
22
- {
23
- h++;
24
- n++;
17
+ int strStr_1 (char *haystack, char *needle) {
18
+ int sLen = strlen (haystack);
19
+ const int tLen = strlen (needle);
20
+ if (tLen == 0 ) return 0 ;
21
+ if (haystack==NULL || needle==NULL || sLen ==0 ) return -1 ;
22
+ int i = 0 , j =0 ;
23
+ while (i < sLen ) {
24
+ j = 0 ;
25
+ int k = i;
26
+ while (j < tLen && haystack[k] == needle[j]) {
27
+ ++k, ++j;
28
+ }
29
+ if (j == tLen) return k - j;
30
+ ++i;
31
+ }
32
+ return -1 ;
33
+ }
34
+ void getNext (char * T, int next[], int n){
35
+ int i=0 , j=-1 ;
36
+ next[0 ]=-1 ;
37
+ while (i<n){
38
+ while (j>-1 &&T[j]!=T[i]) j = next[j];
39
+ i++,j++;
40
+ if (T[j]==T[i]) next[i]=next[j];
41
+ else next[i]=j;
42
+ }
43
+ }
44
+ int strStr_2 (char *haystack, char *needle) {
45
+ int sLen = strlen (haystack);
46
+ const int tLen = strlen (needle);
47
+ if (tLen == 0 ) return 0 ;
48
+ if (haystack==NULL || needle==NULL || sLen ==0 ) return -1 ;
49
+
50
+ int next[tLen+1 ];
51
+ getNext (needle,next,tLen);
52
+
53
+ int i=0 , j=0 ;
54
+ while (i<sLen ){
55
+ while (j>-1 &&needle[j]!=haystack[i]) j = next[j];
56
+ i++,j++;
57
+ if (j==tLen){
58
+ return i - j;
59
+ }
60
+ }
61
+ return -1 ;
62
+ }
63
+ int strStr_3 (char *haystack, char *needle) {
64
+ if (needle && *needle == ' \0 ' ) return 0 ;
65
+ if (haystack == NULL || needle == NULL || *haystack == ' \0 ' ) return -1 ;
66
+ long long fh = 0 , fn = 0 ;
67
+ char *n = needle, *tail = haystack, *head = haystack;
68
+ while (*n) {
69
+ if (*tail == ' 0' ) return -1 ;
70
+ fh += *tail;
71
+ fn += *n;
72
+ ++n, ++tail;
73
+ }
74
+ --tail;
75
+ while (*tail) {
76
+ if (fn == fh) {
77
+ char *pTemp = head,*nTemp = needle;
78
+ while (*nTemp && *nTemp == *pTemp) {
79
+ ++nTemp; ++pTemp;
80
+ }
81
+ if (*nTemp == ' \0 ' ) return head - haystack;
25
82
}
26
- if (*n == ' \0 ' ) return haystack;
27
- if (*h == ' \0 ' ) return NULL ;
28
- haystack++;
83
+ fh -= *head;
84
+ ++tail;
85
+ ++head;
86
+ fh += *tail;
29
87
}
88
+ return -1 ;
30
89
}
31
- };
90
+ };
0 commit comments