-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0028_Implement_strStr.py
More file actions
28 lines (26 loc) · 883 Bytes
/
0028_Implement_strStr.py
File metadata and controls
28 lines (26 loc) · 883 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
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
# Time: O(N*H)
# Space: O(1)
if not needle:
return 0
# Let's suppose this function does not exist
# return haystack.find(needle)
H = len(haystack)
N = len(needle)
i = 0
while i < H:
if haystack[i] == needle[0]:
n = 1
h = i + 1
while n < N and h < H:
if haystack[h] != needle[n]:
break
n += 1
h += 1
if n == N:
return i
i += 1
return -1
# Runtime: 36 ms, faster than 82.04% of Python3 online submissions for Implement strStr().
# Memory Usage: 13.8 MB, less than 63.66% of Python3 online submissions for Implement strStr().