forked from shichao-an/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution2.py
More file actions
26 lines (23 loc) · 623 Bytes
/
solution2.py
File metadata and controls
26 lines (23 loc) · 623 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
"""
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
needle: | n |
haystack: | n - m | m |
"""
n = len(haystack)
m = len(needle)
for i in range(n + 1 - m):
for k in range(m):
if haystack[i + k] != needle[k]:
break
else:
return i
return -1