28. Implement strStr()
KMP算法
problem description
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Example 2:
Clarification:
What should we return when needle
is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle
is an empty string. This is consistent to C's strstr() and Java's indexOf().
algorithm thought
这题其实暴力发就能解决,就是单纯的字符串匹配。但是太慢了,历史上也有几位大佬觉得这种方法很慢。并且确实有可以优化的地方,如果是一个人来匹配字符串,当一个成功了很多次之后最后一个字符匹配失败了,他不会前进一位重新来。而是跳过已知的不匹配的情况。
我描述肯定不是很好懂,可以看看这个网站的例子:https://subetter.com/algorithm/kmp-algorithm.html
kmp最关键的是构建next数组,然后就很好匹配了。
code
algorithm analysis
暴力法两个循环,heystack字符串长m,needle字符串长n,时间复杂度是O(nm)
kmp算法,求出next数组时间复杂度是O(n),匹配时间复杂度是O(m),m>n。最后时间复杂度是O(m)
Last updated