Mental Model
KMP finds a pattern in text by remembering where to restart after a mismatch, so it never rechecks characters unnecessarily.
Analogy: Imagine searching a word in a book. Instead of starting over after a mismatch, you use a bookmark that tells you where to continue, saving time.
Text: A B C D A B C D A B C
Pattern: A B C D A B
↑
Prefix table: [0,0,0,0,1,2]