The Z algorithm needs constant time lookups so moving to an imperative implementation.