forked from y-ncao/Python-Study
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplement_strStr.py
More file actions
Latest commit
35 lines (33 loc) · 1.04 KB
/
Copy pathImplement_strStr.py
File metadata and controls
35 lines (33 loc) · 1.04 KB
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
"""
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
"""
classSolution:
# @param haystack, a string
# @param needle, a string
# @return a string or None
defstrStr(self, haystack, needle):
H=len(haystack)
N=len(needle)
ifN==0:
returnhaystack
i=0
whilei<H-N+1:
ifhaystack[i] ==needle[0]:
start=None# Use None here
j=1
whilej<Nandhaystack[i+j] ==needle[j]:
ifstart==Noneandhaystack[i+j] ==needle[0]: # Find first dup occurance
start=i+j
j+=1
ifj==N:
returnhaystack[i:]
ifstartisnotNone:
i=start
else:
i=i+j
else:
i+=1
returnNone
# Note:
# line 32, don't forget the i += 1