AI engineering glossary · Data structures algorithms

What is KMP (Knuth-Morris-Pratt algorithm)?

Knuth-Morris-Pratt algorithm (also called KMP): A string matching algorithm that precomputes, for the pattern, how far to fall back after a mismatch so that no text character is examined twice. It searches in O(n + m) time.

Level: advancedTopic: Data structures algorithms

Knuth-Morris-Pratt algorithm: an example

Searching 'ABABC' in a text, after a mismatch it jumps using the pattern's prefix table rather than restarting from the next text character.

Related terms

More Data structures algorithms terms

Where you learn it

Knuth-Morris-Pratt algorithm is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.