Mikhail Melnik's solution may not compute Z for all indexes in a string like "aaaaa" we need an additional iteration to fill the indexes which are left empty in the first iteration.
for i in range(0, len(s)):
Z[i - lps[i] + 1] = lps[i]
for i in range(0, len(s)):
Z[i] = max(Z[i], Z[i - 1] - 1) `