594.strStr II
1.Description(Hard)
ImplementstrStr
function in O(n + m) time.
strStr
return the first index of the target string in a source string. The length of the target string ism_and the length of the source string is n.
If target does not exist in source, just return -1.
Example
Given source =abcdef
, target =bcd
, return1
.
2.Code
O(n + m)的算法有2个: KMP和Rabin-Karp。
KMP有个问题,因为DFA的方法预处理的表的空间为R * M,所以memory会越界。
http://wiki.jikexueyuan.com/project/kmp-algorithm/define.html
还有一个best performance是O(N/M)但是worst case是O(MN)的算法Boyer-Moore。
https://xuezhashuati.blogspot.ca/2017/03/lintcode-594-strstr-ii.html
Last updated