-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_similarity_v2.py
More file actions
executable file
·45 lines (35 loc) · 1007 Bytes
/
string_similarity_v2.py
File metadata and controls
executable file
·45 lines (35 loc) · 1007 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
import os
import sys
from pathlib import Path
from typing import IO
# cf. https://www.hackerrank.com/challenges/string-similarity/topics/z-function
def zFunction(s: str) -> list[int]:
N = len(s)
z = [0] * N
m_l = m_r = 0
for i in range(1, N):
if i <= m_r:
z[i] = min(m_r - i + 1, z[i - m_l])
while i + z[i] < N and s[z[i]] == s[i + z[i]]:
z[i] += 1
if i + z[i] - 1 > m_r:
m_l = i
m_r = i + z[i] - 1
return z
def stringSimilarity(s: str) -> int:
z = zFunction(s)
return sum(z) + len(s)
def main(fptr: IO) -> None:
t = int(input().strip())
for _ in range(t):
s = input()
result = stringSimilarity(s)
fptr.write(str(result) + "\n")
if __name__ == "__main__":
if path := os.getenv("OUTPUT_PATH"):
with Path(path).open("wt", encoding="utf-8") as fptr:
main(fptr)
fptr.close()
else:
main(sys.stdout)