A palindrome is a string that reads the same forwards and backwards: racecar, level, abacaba. Finding whether one hides inside a longer string sounds like a simple scan — until you realise you might have to check every possible center and expand in both directions, costing time on an n-character string.
In 1975, Glenn Manacher published a one-page algorithm that solved the problem in — strictly linear, no matter the input. The idea is almost embarrassingly elegant: if you already know a palindrome spans positions l to r, then any character inside it has a mirror on the other side. You already know how far the mirror's palindrome reaches. Why expand from scratch?
Manacher's algorithm keeps a running rightmost palindrome boundary and reuses every previously computed radius. Each character is visited at most twice — once when the boundary moves right, once when it is mirrored — so the total work stays linear. It is a case study in how a single structural insight collapses an apparently quadratic problem to linear.
Comments
Loading comments...