In [2]:
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
nee_len = len(needle)
hay_len = len(haystack)
if nee_len > hay_len:
return -1
for i in range(hay_len - nee_len + 1):
for j in range(nee_len):
if haystack[i + j] != needle[j]:
break
else:
return i
return -1
In [3]:
sol = Solution()
print("Output is :- ",sol.strStr("sadbutsad","sad"))
print("Output is :- ",sol.strStr("leetcode","leeto"))
print("Output is :- ",sol.strStr("safbutsadasf","sad"))
Output is :- 0 Output is :- -1 Output is :- 6