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