class Solution: def strStr(self, haystack: str, needle: str) -> int: next = self.same_start_end_str(needle) #hi是haystack当前索引,ni是needle当前索引 hi = ni = 0 hl = len(haystack) nl = len(needle) while hi < hl and ni < nl: if ni == -1 or haystack[hi] == needle[ni]: hi += 1 ni += 1 else: ni = next[ni] if ni == nl: return hi - ni else: return -1 @staticmethod