KMP
and Z
algorithms are well known algorithms for string searching,
KMP
algorithm deals with finding the patterns through a KMP f
NOTE: algorithm is wrong
for i in range(0, len(s)):
if lps[i] != 0:
Z[i - lps[i] + 1] = lps[i]
After that in Z[i]
will be the maximum length of the suffix, that starts in position i
and which is also a prefix of the string.
EDIT
As nikhil_vyas noted, the proposed algorithm does not solve your problem. What it actually does is partially filling Z
array with the longest suffixes and some others. Such incomplete array can basically help you to solve several "find the longest something in the string" problems, but it does not answer your question.
The easiest way to rebuild Z
array having lps
array that comes to my mind is to build the string, corresponding to the lps
array and then build Z
array for that string. But I am not sure whether it suits your definition of "some modifications in the lps
array".