Copy
Trading Bots
Events
More

Manacher's Algorithm: O(N) Longest Palindrome

2026/07/16 02:36Browse 0

A new implementation of Manacher's algorithm achieves 99.44% runtime performance by leveraging mirror boundary optimization. The algorithm finds the longest palindromic substring in O(n) time and O(n) space, making it one of the most efficient solutions for this classic problem.

How the Algorithm Works

The key insight of Manacher's algorithm is exploiting palindrome symmetry to avoid redundant comparisons. Instead of treating odd- and even-length palindromes separately, the input string is transformed by inserting a special character (#) between every character and adding sentinel characters (^ and $) at both ends. This transformation allows every palindrome to be treated as an odd-length palindrome, simplifying the logic.

During traversal of the transformed string, the algorithm maintains two variables: the center and right boundary of the rightmost palindrome found so far. For each position, it uses the palindrome information from its mirror position relative to the current center to initialize the palindrome radius. This initialization step significantly reduces unnecessary expansions. Only when the palindrome reaches beyond the current right boundary is additional expansion performed.

Implementation Details

The algorithm handles edge cases by returning an empty string if the input is empty. It creates a palindrome radius array p, where p[i] stores the radius of the palindrome centered at index i in the transformed string. Variables center and right track the current rightmost palindrome, while max_len and center_index record the longest palindrome found.

For each position i (ignoring sentinels), the algorithm computes the mirror index using the current center. If i lies within the current right boundary, it initializes p[i] using the minimum of (right - i) and p[mirror]. It then expands around i while characters on both sides match, updating the radius. If the expanded palindrome extends beyond right, center and right are updated. When a longer palindrome is found, max_len and center_index are updated accordingly.

Finally, the algorithm converts the center position from the transformed string back to the original string's starting index using the formula (center_index - max_len) // 2, and returns the substring from start to start + max_len.

Performance and Complexity

The time complexity is O(n) because each character in the transformed string is expanded at most once beyond the current right boundary. The mirror optimization eliminates redundant comparisons, giving a linear runtime. Space complexity is O(n) due to the transformed string and palindrome radius array.

The algorithm's efficiency makes it suitable for large inputs, and the provided Python implementation demonstrates clean, readable code that achieves top-tier performance benchmarks.

Disclaimer: This page may contain third-party information and does not necessarily reflect BYDFi's views or opinions. This content is for general reference only and does not constitute any representation, warranty, financial advice, or investment advice. BYDFi is not responsible for any errors, omissions, or any results arising from the use of such information. Virtual asset investments involve risks. Please carefully evaluate the risks of the product and your risk tolerance based on your financial situation. For more information, please refer to our Terms of Use and Risk Disclosure.