forked from coldmanck/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0028_Implement_strStr().py
49 lines (46 loc) · 1.55 KB
/
0028_Implement_strStr().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
class Solution:
def strStr(self, haystack, needle):
def compute_fail_func(needle):
fail_func = [0]
i, j = 1, 0
n_prefix = 0
while i < len(needle):
while i < len(needle) and needle[i] == needle[j]:
n_prefix += 1
fail_func.append(n_prefix)
i, j = i + 1, j + 1
n_prefix = 0
fail_func.append(n_prefix)
i, j = i + 1, 0
return fail_func
fail_func = compute_fail_func(needle)
# print(fail_func)
i, j = 0, 0
while i < len(haystack):
orig_i = i
while i < len(haystack) and j < len(needle) and haystack[i] == needle[i]:
i, j = i + 1, j + 1
if i == len(haystack):
return -1
if j == len(needle):
return orig_i
if j > 0:
j = fail_func[j - 1]
return -1
# Time O(nm)
# def strStr(self, haystack: str, needle: str) -> int:
# if not needle:
# return 0
# for i in range(len(haystack)):
# j, k = i, 0
# while j < len(haystack) and k < len(needle) and haystack[j] == needle[k]:
# j, k = j + 1, k + 1
# if k == len(needle):
# return i
# if j == len(haystack):
# return -1
# return -1
sol = Solution()
haystack = 'atcamalgamaokvlfpeifjvnbdfs'
needle = 'amalgamation'
sol.strStr(haystack, needle)