First, the topic
Implement strStr ().
Returns the index of the first occurrence of needle in haystack, or-1 if needle are not part of haystack.
Second, analysis
It's easy to think of an O (m*n) algorithm, and it's easy to implement. The original string s, pattern string p, uses the [I:j] method in Python to match the length Len (p) element in the original string. If it matches, return the head, or move right. This complexity is O (m*n)
The quickest is KMP, is O (m+n), looked at the afternoon only to understand a probably. No longer use large chunks of time to see, the morning sporadic time, after all, time is too tense.
Third, the Code
1.O (M*n)
classsolution (object):defstrStr (self, haystack, needle):""": Type Haystack:str:type needle:str:rtype:int"""Len_needle=len (needle) Len_haystack=Len (haystack)ifLen_needle = =0:return0ifLen_haystack = =0:return-1I=0 whileI <= Len_haystack-Len_needle:ifHaystack[i:i + len_needle] = =Needle:returnIElse: I+ = 1return-1
[String match, KMP] Implement StrStr ()