[LeetCode][python3]0028. Implement strStr()

class Solution:
def strStr(self, haystack: str, needle: str) -> int:
#return haystack.find(needle)
if not needle:
return 0

for i in range(len(haystack)-len(needle)+1):
if haystack[i:i+len(needle)]==needle:
return i
return -1
N2I -2020.03.23
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
#return haystack.find(needle)
if not needle:
return 0
hay_len=len(haystack)
nee_len=len(needle)
for cs in range(len(haystack)):
pt=0
h=cs
while cs<hay_len and pt<nee_len and needle[pt] == haystack[cs]:
pt+=1
cs+=1
if pt==nee_len:
return h
return -1

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store