notesonly.in

One notebook for every subject — open it anywhere.

Log in

String matching algorithms (KMP, Rabin-Karp)

Design and Analysis of Algorithms · Engineering

Study notes

Search 'ABAB' in 'ABABABAB' with KMP: prefix function for pattern is [0,0,1,2]. Match ABAB at 0-3; on mismatch at position 4, instead of restarting, the prefix table shifts the pattern by 2 (overlap 'AB'), continuing in O(n). Naive would recheck from position 1.

← Back to topics for Engineering