![]() |
VOOZH | about |
Manacher’s Algorithm is an algorithm used to find all palindromic substrings of a string in linear time. It is mainly applied to problems involving palindromes, especially when fast processing is needed.
It is commonly used to solve:
Traditional methods like brute-force or dynamic programming take O(n²) time and Rabin Karp (Rolling Hash) take O(n × log n) time which is inefficient for large strings or multiple queries. Manacher’s Algorithm solves these problems efficiently in O(n) time.
Before diving into Manacher’s Algorithm, let’s briefly look at common approaches used for palindrome-related problems and why they fall short in terms of efficiency.
Brute-Force
Dynamic Programming (DP)
Center Expansion
Rabin-Karp (Hashing) + Binary Search
All the above methods are slower than linear time. Manacher’s Algorithm solves the same problems in O(n) time using a clever technique based on symmetry and center expansion, with constant space (excluding the result array).
Why Preprocessing is Needed ?
In a normal string, palindromic substrings can be of odd or even length:
Handling these two cases separately makes the implementation complex. To simplify the logic and treat all palindromes uniformly, Manacher’s Algorithm modifies the original string so that:
This avoids writing separate code for odd and even lengths and allows a single unified expansion logic.
How It’s Done in Code:
Example:
For input string: s = "abba"
Transformed string ms: "@#a#b#b#a#$"
Each character (including #) can now be treated as a potential center for a palindrome. This transformation simplifies the core logic of the algorithm and ensures consistent behavior.
After preprocessing, the algorithm runs a single loop over the transformed string ms to compute the array p[], where p[i] stores the radius (number of characters on one side) of the longest palindrome centered at position i.
This logic is implemented in the runManacher() function.
We now explain how code handles the three mirror symmetry cases:
When i < r → i is within current palindrome:
int mirror = l + r - i;
p[i] = min(r - i, p[mirror]);
This mirrors the idea of computing the palindrome at i by referencing its mirror position j = 2*c – i.
We are essentially handling Case 1 and Case 2:
Case 1: (p[mirror] < r - i): Mirror is fully inside current palindrome → safe to copy full value.
Case 2: (p[mirror] > r - i): Mirror extends beyond r, so copy only up to boundary r.
p[i] = min(r - i, p[mirror]);
which correctly handles both cases using min.
When p[mirror] == r - i (Case 3):
We still set p[i] = r - i via the min(...) logic above, but...
We continue expanding manually with:
This is the implementation of Case 3, where the mirrored palindrome just touches the boundary, and you try to expand beyond it.
Update of [l, r] interval: Once expansion at i goes past r, we update the rightmost palindrome. This keeps l and r as the bounds of the longest palindrome seen so far, required for correct mirror computations in the next iterations.
To understand why Manacher’s Algorithm correctly computes all palindromic substrings in linear time, we need to look at the core idea of symmetry and how it avoids redundant work using mirror positions.
Symmetry in Palindromes
A palindrome has mirror symmetry around its center.
If a palindrome is centered at some position center, and we are at a position i inside that palindrome (i.e., i < r), then the character at i has a mirror position: mirror = 2 * center - i
Reusing Information from Mirror
If we already know that p[mirror] = x, we can safely say that:
Time Complexity: O(n)
Manacher’s Algorithm runs in linear time because each character in the transformed string is visited at most once during expansion, and mirror values prevent redundant checks. In total, the algorithm performs O(n) operations where n is the length of the original string (after transformation it's about 2n+3, but still linear).
Auxiliary Space: O(n)
An extra array p[] of size proportional to the transformed string (i.e., 2n+3) is used to store palindrome radii. The transformed string also takes O(n) space, so the total additional space remains linear in terms of the original string size.