Manacher's Longest Palindromic Substring
Linear-time palindrome radii through mirror symmetry
Give odd and even palindromes one representation
Insert a separator before, after, and between all original characters. Both odd-length and even-length palindromes then have a single center in the transformed string. Let p[i] be the radius around transformed center i.
Reuse a mirror inside the rightmost palindrome
Maintain center C and right boundary R of the palindrome reaching farthest right. When i < R, its mirror is 2C - i. Start p[i] from the smaller of the mirror radius and R - i, then compare only characters beyond the known boundary.
The player transforms babad into #b#a#b#a#d#. It marks the active center, mirror, current rightmost box, radius values, and longest palindrome found so far.
Insert separators to transform "babad" into "#b#a#b#a#d#", so every palindrome has one center.
Why all expansions remain linear
Work copied from a mirror costs constant time. Any successful comparison beyond the known box advances R, which can move right only across the transformed string once. Total time and radius storage are therefore O(n).
[C - p[C], R] is the computed palindrome with the greatest right boundary. Contrast palindrome symmetry with rolling windows in Rabin-Karp and prefix fallback in KMP.