28. Implement strStr()
π© Easy
Question
Input: haystack = "hello", needle = "ll"
Output: 2Input: haystack = "aaaaa", needle = "bba"
Output: -1Complexity
Code
Last updated
π© Easy
Input: haystack = "hello", needle = "ll"
Output: 2Input: haystack = "aaaaa", needle = "bba"
Output: -1Last updated
def strStr(self, haystack: str, needle: str) -> int:
lenHaystack = len(haystack)
lenNeedle = len(needle)
if lenHaystack < lenNeedle:
return -1
elif lenNeedle == 0:
return 0
for i in range(lenHaystack):
if haystack[i:i+lenNeedle] == needle:
return i
return -1