In computer science, the longest palindromic substring or longest symmetric factor problem is the problem of finding a maximum-length contiguous substring of a given string that is also a palindrome. For example, the longest palindromic substring of "bananas" is "anana". The longest palindromic substring is not guaranteed to be unique; for example, in the string "abracadabra", there is no palindromic substring with length greater than three, but there are two palindromic substrings with length three, namely, "aca" and "ada". In some applications it may be necessary to return all maximal palindromic substrings (that is, all substrings that are themselves palindromes and cannot be extended to larger palindromic substrings) rather than returning only one substring or returning the maximum length of a palindromic substring. Manacher (1975) invented an O ( n ) {\displaystyle O(n)} -time algorithm for listing all the palindromes that appear at the start of a given string of length n {\displaystyle n} . However, as observed e.g., by Apostolico, Breslauer & Galil (1995), the same algorithm can also be used to find all maximal palindromic substrings anywhere within the input string, again in O ( n ) {\displaystyle O(n)} time. Therefore, it provides an O ( n ) {\displaystyle O(n)} -time solution to the longest palindromic substring problem. Alternative O ( n ) {\displaystyle O(n)} -time solutions were provided by Jeuring (1994), and by Gusfield (1997), who described a solution based on suffix trees. A faster algorithm can be achieved in the word RAM model of computation if the size σ {\displaystyle \sigma } of the input alphabet is in 2 o ( log n ) {\displaystyle 2^{o(\log n)}} . In particular, this algorithm runs in O ( n log σ / log n ) {\displaystyle O(n\log \sigma /\log n)} time using O ( n log σ / log n ) {\displaystyle O(n\log \sigma /\log n)} space. Efficient parallel algorithms are also known for the problem. The longest palindromic substring problem should not be confused with the different problem of finding the longest palindromic subsequence.
Slower algorithm This algorithm is slower than Manacher's algorithm, but is a good stepping stone for understanding Manacher's algorithm. It looks at each character as the center of a palindrome and loops to determine the largest palindrome with that center. The loop at the center of the function only works for palindromes where the length is an odd number. The function works for even-length palindromes by modifying the input string. The character '|' is inserted between every character in the inputs string, and at both ends. So the input "book" becomes "|b|o|o|k|". The even-length palindrome "oo" in "book" becomes the odd-length palindrome "|o|o|".
The runtime of this algorithm is O ( n 2 ) {\displaystyle O(n^{2})} . The outer loop runs n {\displaystyle n} times and the inner loop can run up to n / 2 {\displaystyle n/2} times.
Manacher's algorithm Below is the pseudocode for Manacher's algorithm. The algorithm is faster than the previous algorithm because it exploits when a palindrome happens inside another palindrome. For example, consider the input string "abacaba". By the time it gets to the "c", Manacher's algorithm will have identified the length of every palindrome centered on the letters before the "c". At the "c", it runs a loop to identify the largest palindrome centered on the "c": "abacaba". With that knowledge, everything after the "c" looks like the reflection of everything before the "c". The "a" after the "c" has the same longest palindrome as the "a" before the "c". Similarly, the "b" after the "c" has a longest palindrome that is at least the length of the longest palindrome centered on the "b" before the "c". There are some special cases to consider, but that trick speeds up the computation dramatically.
… excerpt ends here. Continue reading the full article.
