Why it matters

KMP is elegant linear string search. Foundation of many string libs.

Advertisement

The architecture

Build failure table.

Scan text with rewinds via table.

O(n + m).

KMP algorithmFailure tablefrom patternScan textno backtrackO(n + m)linear timeKnuth-Morris-Pratt 1977; failure function encodes proper suffix-prefix
KMP algorithm.
Advertisement

How it works end to end

KMP 1977.

Failure table.

No backtracking.