Skip to main content
Back to the DSA atlas
Strings Algorithm Advanced

Manacher

Find the longest palindromic substring in linear time by reusing information from the rightmost known palindrome.

stringpalindromerare

Interactive visualization

Use the controls to connect the idea to concrete operations before diving into the full write-up.

^#a#b#a#c#a#b#a#$

Palindrome radius

6

Palindromes around every center in linear time

Center on c in the transformed string; the palindrome expands symmetrically outward.

Family

Strings

Prefix-aware structures and text-centric search strategies.

Builds on

Foundational

You can start here without another page first.

Unlocks

0 next topics

Use these follow-ups when you want to apply the current idea in a richer setting.

Learning paths

2

This topic appears in curated progressions so you can keep moving without guessing the next step.

Problem

You want the longest palindromic substring, or the palindrome radius around every center.

A naive expand-around-center solution takes O(n2)O(n^2) in the worst case. Manacher reduces that to linear time.

Core idea

Insert separators so even- and odd-length palindromes become one uniform case, then maintain the rightmost palindrome seen so far.

For a new center i inside that known palindrome, use its mirror position to get a guaranteed initial radius before doing any explicit comparisons.

That reuse is what kills the quadratic blowup.

Why it matters

Manacher is one of the classic “looks magical until you see the invariant” algorithms.

It teaches a powerful pattern:

once a symmetric region is known, interior positions can borrow information from mirrored positions

Complexity

OperationTime
All palindrome radii / longest palindromic substringO(n)O(n)

Key takeaways

  • Manacher is the linear-time answer to longest palindromic substring
  • The transformed string removes the even/odd split
  • The mirror trick reuses previously known palindrome coverage
  • This is a rare string algorithm, but it belongs in a truly complete atlas

Practice problems

ProblemDifficultyKey idea
Longest Palindromic Substring🟡 MediumManacher or center expansion
Palindromic Substrings🟡 MediumCount via radius information
Manacher references🔴 HardFull linear-time derivation

Relation to other topics

  • Z-Algorithm is another linear string routine based on reusing a rightmost interval
  • Rabin-Karp can also test palindrome candidates, but through hashing instead of direct radius reuse
  • Suffix Automaton is another advanced string structure for substring-heavy problems

Related directions

These topics live nearby conceptually, even if they are not direct prerequisites.

More from Strings

Stay in the same family when you want parallel variations of the same mental model.

Paths that include this topic

Follow one of these sequences if you want a guided next step instead of open-ended browsing.

String matching

Move from prefix reuse and rolling hashes to suffix-based indexing and automata.

Substring toolkit

Compare rolling hashes, sorted suffixes, automata, and palindrome-specific structure.

From the blog

Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.