-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path28. Implement strStr().py
65 lines (55 loc) · 1.37 KB
/
28. 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution:
def COMPUTE_PREFIX_FUNCTION(self, P):
m = len(P)
pi_array = [0 for _ in range(m+1)]
k = 0
for q in range(2,m+1):
while k>0 and P[k]!=P[q-1]:
k = pi_array[k]
if P[k] == P[q-1]:
k+=1
pi_array[q] = k
return pi_array[1:]
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
S = haystack
P = needle
sLen = len(S)
pLen = len(P)
i = 0
j = 0
pi_array = self.COMPUTE_PREFIX_FUNCTION(P)
while i<sLen and j<pLen:
if j==-1 or S[i]==P[j]:
i += 1
j += 1
else:
j = pi_array[j]-1
# i = i-1
if j!=-1:
print(S[i], P[j])
if j==pLen:
return i-j
else:
return -1
import time
start = time.clock()
s = "ababaca"
ExpectOutput = [0,0,1,2,3,0,1]
print(Solution().COMPUTE_PREFIX_FUNCTION(s))
str = "mississippi"
pattern = "issip"
ExpectOutput = 4
solu = Solution() # 先对类初始化,才能进行调用
temp = solu.strStr(str, pattern)
if (temp == ExpectOutput):
print('right')
else:
print('wrong')
print(temp)
elapsed = (time.clock() - start)
print("Time used:", elapsed)