Implement strStr()

2014.2.28 02:38

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

Solution1:

  The first one is brute-force, reference is from source code of strstr() in <string.h>.

  Total time complexity is O(n * m). Space complexity is O(1).

Accepted code:

 1 // 1WA, 1AC, standard Brute-Force solution.
 2 #include <cstring>
 3 using namespace std;
 4 
 5 class Solution {
 6 public:
 7     char *strStr(char *haystack, char *needle) {
 8         const char *ptr = haystack;
 9         size_t len = strlen(needle);
10         if (len == 0) {
11             return haystack;
12         }
13         while ((ptr = strchr(ptr, *needle)) != nullptr) {
14             if (strncmp(ptr, needle, len) == 0) {
15                 return (char *)ptr;
16             }
17             ++ptr;
18         }
19     }
20 };

Solution2:

  This solution uses KMP Algorithm, which runs in linear time.

  The key difference between KMP and Brute-Force is the "next" array, that defines the position to backtrack when mismatch happens.

  Here is the reference from Wikipedia of KMP Algorithm, if you'd like to learn more about it.

  Total time complexity is O(len(haystack) + len(needle)). Space complexity is O(needle).

Accepted code:

 1 // 2CE, 1RE, 1WA, 1AC, KMP Algorithm
 2 #include <cstring>
 3 #include <vector>
 4 using namespace std;
 5 
 6 class Solution {
 7 public:
 8     char *strStr(char *haystack, char *needle) {
 9         if (haystack == nullptr || needle == nullptr) {
10             return nullptr;
11         }
12         lp = strlen(needle);
13         lw = strlen(haystack);
14         
15         if (lp == 0) {
16             return haystack;
17         }
18         
19         if (lw < lp) {
20             return nullptr;
21         }
22         
23         calculateNext(needle);
24         char *result = KMPMatch(haystack, needle);
25         next.clear();
26         
27         return result;
28     }
29     int lp;
30 private:
31     int lw;
32     vector<int> next;
33     
34     void calculateNext(char *pat) {
35         int i = 0;
36         int j = -1;
37         
38         next.resize(lp + 1);
39         next[0] = -1;
40         while (i < lp) {
41             if (j == -1 || pat[i] == pat[j]) {
42                 ++i;
43                 ++j;
44                 next[i] = j;
45             } else {
46                 j = next[j];
47             }
48         }
49     }
50     
51     char* KMPMatch(char *word, char *pat)
52     {
53         int index;
54         int pos;
55         
56         index = pos = 0;
57         while (index < lw) {
58             if (pos == -1 || word[index] == pat[pos]) {
59                 ++index;
60                 ++pos;
61             } else {
62                 pos = next[pos];
63             }
64             
65             if (pos == lp) {
66                 // the first match is found
67                 return word + (index - lp);
68             }
69         }
70         
71         return nullptr;
72     }
73 };

 

 posted on 2014-02-28 02:42  zhuli19901106  阅读(257)  评论(0编辑  收藏  举报