-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplement strStr().py
More file actions
37 lines (33 loc) · 885 Bytes
/
Copy pathImplement strStr().py
File metadata and controls
37 lines (33 loc) · 885 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
31
32
33
34
35
36
37
__author__ = 'Martin'
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
i = 0
j = 0
flag = i
if needle == "":
return 0
if haystack is "":
return -1
while i < len(haystack):
if haystack[i] == needle[j]:
flag = i
while j < len(needle):
if i >= len(haystack):
return -1
if haystack[i] != needle[j]:
j = 0
i = flag
break
i += 1
j += 1
if j == len(needle):
return flag
i += 1
return -1
s = Solution()
print(s.strStr("a", ""))