Manacher's Algorithm Solves Longest Palindrome in Linear Time with Mirror Trick

A developer has shared an O(N) implementation of Manacher's Algorithm for finding the longest palindromic substring, reportedly outperforming 99.44% of solutions on the platform. The approach transforms the input string by inserting special characters between each letter, allowing both odd- and even-length palindromes to be handled uniformly. A mirror-based optimization lets the algorithm reuse previously computed palindrome radii, avoiding redundant character comparisons during traversal. The rightmost palindrome's center and boundary are tracked throughout, ensuring each character is expanded at most a constant number of times. Both time and space complexity are O(N), accounting for the transformed string and the palindrome radius array.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in