-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28.py
More file actions
30 lines (24 loc) · 648 Bytes
/
28.py
File metadata and controls
30 lines (24 loc) · 648 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
30
'''
28. Implement strStr()
Implement strStr().
Returns the index of the first occurrence of needle in
haystack, or -1 if needle is not part of haystack.
'''
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if needle == "":
return 0
if haystack == "":
return -1
interval = len(needle)
for i in range(len(haystack) - interval):
if haystack[i : i + interval] == needle:
return i
else:
i = i + interval
return -1