[Leetcode 83] 28 Implement strStr()

Problem:

Implement strStr().

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

 

Analysis:

There are two ways: one is the naive comparing algorithm and the other is KMP algorithm.

For KMP algorithm, please refer the wiki page

For the naive algorithm, we start from the haystack's first position until the one plus needle's length is greater than the haystack's length.

For each position, we compare it with the needle. If they are the same, we compare the next position and repeat this process. If they are not the same, we know this i-position won't give a match. So we just break out of the loop and go to the next i position. If we reach the end of the needle position, then there is a match. And return the i. This algoritm is of complexity O(m*n)

 A special case is that the given needle is of length 0. In this case, we directly return the haystack.

 

Code:

Naive algorithm:

 1 class Solution {
 2 public:
 3     char *strStr(char *haystack, char *needle) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         int hlen = strlen(haystack);
 7         int nlen = strlen(needle);
 8         
 9         for (int i=0; i+nlen <= hlen; i++) {
10             int j = 0;
11             
12             for (j=0; j<nlen; j++) {
13                 if (haystack[j+i] != needle[j])
14                     break;
15             }
16             
17             if (j == nlen) return haystack+i;
18         }
19         
20         return NULL;
21     }
22 };
View Code

 

KMP algorithm:

 1 class Solution {
 2 public:
 3     char *strStr(char *haystack, char *needle) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         int n = strlen(haystack), m = strlen(needle);
 7         
 8         if (m == 0)
 9             return haystack;
10         
11         failure_function(needle);
12         int i = 0, j = 0;
13         while (true) {
14             if (i == n)
15                 break;
16                 
17             if (haystack[i] == needle[j]) {
18                 i++;
19                 j++;
20                 if (j == m) 
21                     return haystack + i - j;
22             } else if (j > 0) {
23                 j = ff[j];
24             } else
25                 i++;
26         }
27         
28         return NULL;
29     }
30     
31     
32     void failure_function(char *s) {
33         int n = strlen(s);
34         
35         ff = new int[n+1];
36         
37         ff[0] = ff[1] = 0;
38         
39         for (int i=2; i<=n; i++) {
40             int j = ff[i-1];
41             while (true) {
42                 if (s[j] == s[i-1]) {
43                     ff[i] = j + 1;
44                     break;
45                 }
46                 
47                 if (j == 0) {
48                     ff[i] = 0;
49                     break;
50                 }
51                      
52                 j = ff[j];
53             }
54         }
55     }
56     
57 private:
58     int *ff;
59 };
View Code

 

转载于:https://www.cnblogs.com/freeneng/p/3209805.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值