题目描述(简单难度)
解题思路
根据需要匹配的长度来确定滑动的长度
python 代码
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
if not haystack and not needle or not needle:
return 0
length = len(needle)
i = 0
j = length
while j <= len(haystack):
if haystack[i:j] == needle:
return i
else:
i += 1
j += 1
return -1
s = Solution()
res = s.strStr("aaaaa","bba")
print('.....',res)