594.strStr II
Last updated
Was this helpful?
Last updated
Was this helpful?
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
.
O(n + m)的算法有2个: KMP和Rabin-Karp。
KMP有个问题,因为DFA的方法预处理的表的空间为R * M,所以memory会越界。
还有一个best performance是O(N/M)但是worst case是O(MN)的算法Boyer-Moore。