This is just the Z-function of the modified string. A slight difference is that the first element of the Z-function is chosen equal to the length S. There is an algorithm for computing the Z-function of a string in O (n)
And the algorithm for this task is as follows:
- S ': = reverse S
- Z: = Z-function of S '
- for each i, Output [i]: = Z [Len (S) - A [i] - 1]
For instance:
S = "baabaaa" A[] = [0,1,2,3,4,5,6] Output[] should be [0,1,2,0,1,2,7] S' = "aaabaab" Z = Z-function(S') = [7,2,1,0,2,1,0] (with the first element chosen to be Len(S))
source share