-
Notifications
You must be signed in to change notification settings - Fork 0
/
findFirstOccurence.py
51 lines (33 loc) · 1 KB
/
findFirstOccurence.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
import math
from typing import Optional
# [1,2,3,4,5]
# left = 0
# right = 4
# center = 2
# step = 2
# [1,2,3]
# left = 0
# right = 2
# center = 1
# step = 1
class Solution:
def strStr(self, haystack: str, needle: str, left: int = None, right: int = None) -> int:
if left is None:
left = 0
if right is None:
right = len(haystack) - len(needle)
step = (right - left) // 2
if step == 0:
if haystack[left:left+len(needle)] == needle:
return left
elif haystack[right:right+len(needle)] == needle:
return right
else:
return -1
left_result = self.strStr(haystack, needle, left, right - step)
if left_result != -1:
return left_result
return self.strStr(haystack, needle, left + step, right)
assert Solution().strStr("sadbutsad", "sad") == 0
assert Solution().strStr("leetcode", "leeto") == -1
assert Solution().strStr("hello", "ll") == 2