这是Leetcode上的第5题,题目如下:
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
给定一个字符串,找到该字符串的最长连续回文子串。例如输入“babad”,输出结果为“bab”.
Manacher’s Algorithm
这道题有一个时间复杂度为 O(n) 的算法,被称为Manacher’s Algorithm,该算法的思路十分巧妙。
算法来源: http://articles.leetcode.com/longest-palindromic-substring-part-ii/
还参考了这篇博客:http://blog.csdn.net/hopeztm/article/details/7932245
原文的解释不太容易理解,所以我在这里解释这个算法,也帮助自己理解吸收。
算法思路其实就是遍历字符串扩展出以每个字符为中心的最长回文串。但是如果用一般的扩展方法是会达到 O(n2