Search code examples
algorithmpalindromesuffix-tree

Longest palindrome in a string using suffix tree


I was trying to find the longest palindrome in a string. The brute force solution takes O(n^3) time. I read that there is a linear time algorithm for it using suffix trees. I am familiar with suffix trees and am comfortable building them. How do you use the built suffix tree to find the longest palindrome.


Solution

  • I believe you need to proceed this way:

    Let y1y2 ... yn be your string (where yi are letters).

    Create the generalized suffix tree of Sf = y1y2 ... yn$ and Sr = ynyn - 1 ... y1# (reverse the letters and choose different ending characters for Sf ($) and Sr (#))... where Sf stands for "String, Forward" and Sr stands for "String, Reverse".

    For every suffix i in Sf, find the lowest common ancestor with the suffix n - i + 1 in Sr.

    What runs from the root till this lowest common ancestor is a palindrome, because now the lowest common ancestor represents the longest common prefix of these two suffixes. Recall that:

    (1) A prefix of a suffix is a substring.

    (2) A palindrome is a string identical to its reverse.

    (3) So the longest contained palindrome within a string is exactly the longest common substring of this string and its reverse.

    (4) Thus, the longest contained palindrome within a string is exactly the longest common prefix of all pairs of suffixes between a string and its reverse. This is what we're doing here.

    EXAMPLE

    Let's take the word banana.

    Sf = banana$

    Sr = ananab#

    Below is the generalised suffix tree of Sf and Sr, where the number at the end of each path is the index of the corresponding suffix. There's a small mistake, the a common to all the 3 branches of Blue_4's parent should be on its entering edge, beside n:

    enter image description here

    The lowest interior node in the tree is the longest common substring of this string and its reverse. Looking at all the interior nodes in the tree you will therefore find the longest palindrome.

    The longest palindrome is found between between Green_0 and Blue_1 (i.e., banana and anana) and is anana


    EDIT

    I've just found this paper that answers this question.