-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZalgorithm.py
More file actions
31 lines (30 loc) · 973 Bytes
/
Copy pathZalgorithm.py
File metadata and controls
31 lines (30 loc) · 973 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
29
30
31
def match(string) -> int:
pattern = "place:"
arr = z(pattern+"/0"+string)
for i in range(len(arr)):
if arr[i] == len(pattern):return i - (len(pattern) + 1) -1
return -1
def z(string: str) -> list:
z_val = [0] * len(string)
i = left = right = 1
while i < len(string):
if right <= i:
left = right = i
while right < len(string) and string[right] == string[right - left]:
z_val[i] += 1
right += 1
if right == len(string):break
i += 1
else:
i = right
j = left + 1
while j < right and j + z_val[j - left] < right:
z_val[j] = z_val[j - left]
j += 1
if j < right:
left = j
z_val[j] = right - j
while string[right] == string[right - left]:
z_val[j] += 1
right += 1
return z_val