def z_algorithm(s): if not s: return [] n = len(s); z = [0]*n; z[0] = n; i = 1; j = 0 while i