Linear time algorithm for finding the longest palindromic substring
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Longest Subpalindrome (SP) in Linear Time

Implements Manacher's algorithm to find the longest palindromic substring, or subpalindrome (SP). For a full explanation of the algorithm, see:

Usage Notes
  • The algorithm is case-insensitive as all text is converted to lower case first
  • If multiple SPs exist of the same length, the first one is returned
  • Single characters are considered SPs
  • No special treatment is accorded to whitespace

The script can be called from the command line, where the sole argument is the string in which to search.

$ python xyz

$ python abcracecarxyz
Tolstoy Test

The test script calls the algorithm against the Gutenberg text of Leo Tolstoy's War and Peace, in which the longest SP comes from the line "Pierre saw that Platon did not want to understand what the Frenchman was saying, and he looked on without interfering."

$ python
ton did not