Hello guys,
Is there any way to find the shortest period of a string using Z algorithm. I know of one using KMP.
EDIT:
Sorry for stating the question wrongly.
I was solving this. So the point where i got struck was: given a string, find shortest period for each prefix. Can this be solved by Z-algorithm in linear time? And also is there a way to transform Z array to LPS array and vice-versa?
Thanks in advance,
Shivram
UPDATE: SOLVED
This converts Z to LPS. After that we can use the editorial method.
L is a period of the string if Z[L + 1] = N — L and N % L = 0. You are interested in the smallest L with these properties.
Find the first position j that j + z[j] = n and n % j = 0, so the answer will be j.
Auto comment: topic has been updated by Shivram (previous revision, new revision, compare).
Auto comment: topic has been updated by Shivram (previous revision, new revision, compare).
Auto comment: topic has been updated by Shivram (previous revision, new revision, compare).
This is the simplest method I found for similar problem
https://open.kattis.com/problems/powerstrings