3.8.9 · D1String Algorithms

Foundations — Palindrome algorithms — Manacher's algorithm

1,770 words8 min readBack to topic

This page assumes you have seen nothing. We build every letter, bracket, and idea the parent note (Palindrome algorithms — Manacher's algorithm (index 3.8.9)) leans on, in the order they depend on each other. By the end you should be able to read every line of that note without pausing.


1. A string, and its positions

Figure — Palindrome algorithms — Manacher's algorithm

Why does the topic need this? Every later idea — center, radius, mirror — is a statement about indices. If indices are shaky, nothing above them stands.


2. What a palindrome actually is

The picture that matters is symmetry about a center line: fold the string in half and the two halves land exactly on top of each other.

Figure — Palindrome algorithms — Manacher's algorithm

Why the topic needs this: the entire algorithm is "for every possible center, how far does the mirror hold?" You must first be crystal clear on what a center is and that there are two kinds.


3. Center and radius

Figure — Palindrome algorithms — Manacher's algorithm

Why the topic needs this: the array the algorithm builds, , is exactly "the radius at center ." Radius is the quantity that mirrors cleanly between twins — length does not.


4. The # transformation (odd + even → one shape)

Figure — Palindrome algorithms — Manacher's algorithm

Why the topic needs this: it turns two messy cases into one uniform loop. This is the single most reused idea in the reference code (t = '#' + '#'.join(s) + '#').


5. Mirroring an index about a center

Why the topic needs this: symmetry means "point looks like its twin ." To copy the twin's already-computed radius, you must know which index the twin is. That index is .


6. The known window: and

Why the topic needs this: min(P[2C-i], R-i) — the heart of the algorithm — uses both (the wall) and (to find the twin). No , no reuse, no linear time.


7. The tool: min, and why capping at the wall

Why the topic needs this: this single min is what makes the initial guess instead of a fresh count.


8. Amortized cost — why the expand loop is cheap

Why the topic needs this: it is the whole justification of "." Without it you cannot tell why Manacher beats Expand Around Center's worst case of .


How these feed the topic

string s and index from 0

palindrome = symmetry about a center

center and radius

hash transform to t makes all odd

radius array P equals length in s

mirror index i prime = 2C minus i

window C and R the known reach

min of twin and wall gives cheap guess

expand past wall pushes R right

amortized O of n

Manacher algorithm

Related destinations once you are ready: Longest Palindromic Substring, Expand Around Center, and cousins that reuse the same "copy from a known prefix" trick — the Z-Algorithm, the KMP failure function, and the Palindromic Tree (Eertree).


Equipment checklist

Can you state, for s = "abcd", the value of s[2] and ?
and .
Is abba an odd- or even-length palindrome, and where is its center?
Even; the center is between the two bs (no single center box).
What is the radius of the palindrome racecar about its center e?
Radius (rac on each side); length .
Transform abc with the # trick.
(length ).
If a center in has radius , how long is the palindrome in ?
Length (radius in equals length in ).
Center , current point . What is the mirror index ?
.
Window has , and you are at ; the twin promises . What initial radius do you take?
— capped at the wall.
Why is Manacher and not ?
Because expansion only ever pushes rightward, and moves at most times total (amortized linear).