class Solution:
def strStr(self, haystack: str, needle: str) -> int:
'''
:param haystack:
:param needle:
:return:
用python实现字符串匹配问题
(1)如果索要查找的字符串或者子字符串的长度为0,则返回False
(2)如果总字符串的长度小于所要查找的子字符串的长度,则返回False
(3)将i从0到len(father)-len(sub)
如果 father[i:(i+len(sub)]==sub 则返回True,break
否则 i+=1 进入下一次循环
跳出循环后,返回False
'''
if not len(haystack) and not len(needle):
return 0
if not len(haystack):
return -1
if not len(needle):
return 0
if len(haystack) < len(needle):
return -1
for i in range(0, len(haystack) - len(needle) + 1):
if haystack[i:(i + len(needle))] == needle:
return i
return -1