[LeetCode] Implement strStr()

简介: Well, the problem does not aim for an advanced algorithm like KMP but only a clean brute-force algorithm.

Well, the problem does not aim for an advanced algorithm like KMP but only a clean brute-force algorithm. So we can traverse all the possible starting points of haystack (from 0 tohaystack.length() - needle.length()) and see if the following characters in haystack match those of needle.

The code is as follows.

 1 class Solution {
 2 public: 
 3     int strStr(string haystack, string needle) {
 4         int m = haystack.length(), n = needle.length();
 5         if (!n) return 0;
 6         for (int i = 0; i < m - n + 1; i++) {
 7             int j = 0;
 8             for (; j < n; j++)
 9                 if (haystack[i + j] != needle[j])
10                     break;
11             if (j == n) return i;
12         }
13         return -1;
14     }
15 };

Of course, you may challenge yourself implementing the KMP algorithm for this problem.

KMP is a classic and yet notoriously hard-to-understand algorithm. However, I think the following two links give nice explanations. You may refer to them.

  1. KMP on jBoxer's blog;
  2. KMP on geeksforgeeks, with a well-commented C code.

I am sorry that I am still unable to give a personal explanation of the algorithm. I only read it from the two links above and mimic the code in the second link.

My accepted C++ code using KMP is as follows. Well, it also takes 4ms -_-

 1 class Solution {
 2 public:
 3     int strStr(string haystack, string needle) {
 4         int m = haystack.length(), n = needle.length();
 5         if (!n) return 0;
 6         vector<int> lps = kmpProcess(needle);
 7         for (int i = 0, j = 0; i < m; ) {
 8             if (haystack[i] == needle[j]) { 
 9                 i++;
10                 j++;
11             }
12             if (j == n) return i - j;
13             if (i < m && haystack[i] != needle[j]) {
14                 if (j) j = lps[j - 1];
15                 else i++;
16             }
17         }
18         return -1;
19     }
20 private:
21     vector<int> kmpProcess(string& needle) {
22         int n = needle.length();
23         vector<int> lps(n, 0);
24         for (int i = 1, len = 0; i < n; ) {
25             if (needle[i] == needle[len])
26                 lps[i++] = ++len;
27             else if (len) len = lps[len - 1];
28             else lps[i++] = 0;
29         }
30         return lps;
31     }
32 };

 

目录
相关文章
|
6月前
|
算法 C++ 索引
leetcode-28:实现 strStr()(字符串匹配,暴力匹配算法和KMP算法)
leetcode-28:实现 strStr()(字符串匹配,暴力匹配算法和KMP算法)
79 0
|
1月前
【LeetCode 21】28. 实现 strStr()
【LeetCode 21】28. 实现 strStr()
34 0
|
5月前
|
SQL 算法 数据可视化
Leetcode第28题:实现 strStr()【python】
Leetcode第28题:实现 strStr()【python】
LeetCode-28 实现strStr() KMP算法的学习
LeetCode-28 实现strStr() KMP算法的学习
|
算法 安全 Java
LeetCode - #28 实现 strStr()
不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。如果大家有建议和意见欢迎在文末留言,我们会尽力满足大家的需求。
|
算法 Java C语言
leetcode:28.实现strStr()
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
79 0
leetcode 28 找出字符串第一个匹配的下标(KMP实现strStr)
leetcode 28 找出字符串第一个匹配的下标(KMP实现strStr)
89 0
leetcode 28 找出字符串第一个匹配的下标(KMP实现strStr)
|
存储 前端开发 算法
一行代码解决LeetCode实现 strStr()使用JavaScript解题|前端学算法
一行代码解决LeetCode实现 strStr()使用JavaScript解题|前端学算法
163 0
一行代码解决LeetCode实现 strStr()使用JavaScript解题|前端学算法
|
存储
LeetCode 232. Implement Queue using Stacks
使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部。 pop() -- 从队列首部移除元素。 peek() -- 返回队列首部的元素。 empty() -- 返回队列是否为空。
77 0
LeetCode 232. Implement Queue using Stacks
LeetCode 225. Implement Stack using Queues
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈; pop() -- 移除栈顶元素; top() -- 获取栈顶元素; empty() -- 返回栈是否为空
81 0
LeetCode 225. Implement Stack using Queues