Compute-Prefix-Function(P) m = length(P) [1] = 0 ; k must be less than q k = 0 for q = 2 to m ; O(m) amortized while k > 0 and P[k+1] P[q] k = [k] if P[k+1] = P[q] then k = k + 1 ; prefix increased by one [q] = k return