首页 > 其他 > 详细

LeetCode - Implement strStr()

时间:2014-02-28 19:21:32      阅读:394      评论:0      收藏:0      [点我收藏+]

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:

bubuko.com,布布扣
 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 };
bubuko.com,布布扣

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:

bubuko.com,布布扣
 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 };
bubuko.com,布布扣

LeetCode - Implement strStr(),布布扣,bubuko.com

LeetCode - Implement strStr()

原文:http://www.cnblogs.com/zhuli19901106/p/3572745.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!