PythiloLesson 31 of 32 · progress kept in this browser

Never step back with Knuth-Morris-Pratt

Naive matching restarts after every mismatch and can compare the same text characters many times. Knuth-Morris-Pratt first studies the pattern: for each prefix it records the longest proper prefix that is also a suffix.

pattern = "abab"
failure = [0, 0, 1, 2]
# after a full "abab" match,
# "ab" counts toward the next one
Your task

Write find_all(text, pattern) returning every start index of pattern in text, overlaps included, in linear time. A non-empty pattern is guaranteed.