-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.py
More file actions
27 lines (22 loc) · 705 Bytes
/
Copy pathtemplate.py
File metadata and controls
27 lines (22 loc) · 705 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
#!/usr/bin/env python3
from typing import List
class ZFunction:
def __init__(self, s: str, t: str) -> None:
self.s = s
self.t = t
self.z = self.computeZ()
@staticmethod
def z_function(t: str) -> List[int]:
z = [0] * len(t)
l = r = 0
for i in range(1, len(t)):
if i <= r:
z[i] = min(z[i - l], r - i + 1)
while i + z[i] < len(t) and t[z[i]] == t[i + z[i]]:
l, r = i, i + z[i]
z[i] += 1
return z
def computeZ(self):
s, t = self.s, self.t
z = self.z_function(t + "#" + s)
return [i for i in range(len(t) + 1, len(z)) if z[i] == len(t)]