Skip to content

Commit

Permalink
Create needle-in-haystack.py
Browse files Browse the repository at this point in the history
Finds substring needle in haystack
  • Loading branch information
gabedonnan authored Jan 17, 2023
1 parent 70fe85c commit 6538099
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions needle-in-haystack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
rpointer = 0
if len(needle) > len(haystack):
return -1
for lpointer in range(len(haystack)):
rpointer = lpointer
while haystack[rpointer] == needle[rpointer-lpointer]:
rpointer += 1
if rpointer-lpointer > len(needle) - 1:
return lpointer
if rpointer > len(haystack) - 1:
return -1
return -1

0 comments on commit 6538099

Please sign in to comment.