- Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathImplement_StrStr.py
More file actions
Latest commit
29 lines (25 loc) · 767 Bytes
/
Copy pathImplement_StrStr.py
File metadata and controls
29 lines (25 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
@author - Anirudh Sharma
"""
classImplementStrStr:
@staticmethod
defstrStr(haystack: str, needle: str) ->int:
# Base conditions
ifhaystackisNoneorneedleisNone:
return-1
# Special case
ifhaystack==needle:
return0
# Length of the needle
needleLength=len(needle)
# Loop through the haystack and slide the window
foriinrange(len(haystack) -needleLength+1):
ifhaystack[i:i+needleLength] ==needle:
returni
return-1
if__name__=='__main__':
im=ImplementStrStr()
print(im.strStr("hello", "ll"))
print(im.strStr("aaaaa", "bba"))
print(im.strStr("", ""))
print(im.strStr("abc", "c"))