Rabin-Karp

rock, electric guitar, powerful, anthem · 3:51

Listen on 93

Lyrics

[Verse 1]
Rolling hash function got me feeling so fly
Preprocessing patterns with a mathematical eye
Base to the power, modulo the prime
Computing fingerprints one character at a time
Start with the first window, calculate the code
If the hashes match then we're in comparison mode
Character by character, verify it's true
False positives happen but we'll push on through

[Chorus]
Hash and slide, hash and slide
Rolling through the text with algorithmic pride
When the numbers match we gotta double check
Rabin-Karp method keeping searches in check
Hash and slide, hash and slide
Linear time complexity is our guide
Polynomial rolling keeps the engine smooth
Pattern matching with that west coast groove

[Verse 2]
Take the leftmost character, subtract its weight
Add the new one coming, recalculate
Base raised to M minus one, that's our factor
Sliding window moving like a smooth contractor
Multiple patterns? No problem at all
Hash them separately, let the matches fall
Expected linear time when the hash is clean
Worst case quadratic but that's rarely seen

[Chorus]
Hash and slide, hash and slide
Rolling through the text with algorithmic pride
When the numbers match we gotta double check
Rabin-Karp method keeping searches in check
Hash and slide, hash and slide
Linear time complexity is our guide
Polynomial rolling keeps the engine smooth
Pattern matching with that west coast groove

[Bridge]
Choose your base wisely, pick a prime that's large
Avoid collision damage, stay in charge
ASCII values mapped to numbers clean
Most elegant string search you've ever seen
From left to right we roll across the page
Rabin and Karp set the searching stage

[Verse 3]
Fingerprint matching in the digital age
Every substring gets its numeric gauge
Modular arithmetic keeps the numbers tight
Rolling hash magic working day and night
When patterns are plenty and text is long
Rabin-Karp algorithm keeps us strong
Preprocessing once then we search with ease
Multiple matches falling like autumn leaves

[Chorus]
Hash and slide, hash and slide
Rolling through the text with algorithmic pride
When the numbers match we gotta double check
Rabin-Karp method keeping searches in check
Hash and slide, hash and slide
Linear time complexity is our guide
Polynomial rolling keeps the engine smooth
Pattern matching with that west coast groove

[Outro]
From the Bay to LA, algorithms flow
Rabin-Karp technique, now you know
Hash and slide until the search is done
String matching mastery, second to none

← Knuth-Morris-Pratt (KMP) | Boyer-Moore →