Finding the longest palindromic substring is a classic problem of coding interview. This post summarizes 3 different solutions for this problem.
要求:
给定一个字符串S,找出它的最长回文子串。假定S的最大长度为1000,且最长回文子串唯一
难度:中等
分析:
假定字符串s为回文字符串,则在s头部和尾部分别添加相同字符串[x],所得结果s'=[x]s[x]也为回文字符串(论述1)。可使用动态规划方法解决此问题,递推公式便基于此特性。
创建一个布尔二维数组plain,plain[i][j]为true表示原字符串从索引i到索引j这一段子串为回文。而plain[i][j]为true的前提是s.charAt(i)与s.charAt(j)相同,且以下两个条件满足其一:
1. j-i<=2, j=i时必然成立,j-i=1则表示i与j相邻,"aa"、"bb"满足条件
2. j-i>2, 此时须plain[i+1][j-1]为true,即论述1所述
核心算法分析完毕,写一双重循环,索引i由字符串尾部向前遍历,索引j由i向后遍历,每次发现回文字符串时设置二维数组相应的值,并比较其长度是否大于当前的maxLen。若大于,则更新用于记录当前最长回文子串起始和终止位置的start和end
解决方案:
Java - 396ms
- public String longestPalindrome(String s) {
- if(s.length()<=1){
- return s;
- }
- int start=0, end=0;
- int maxLen = 0;
- boolean[][] plain = new boolean[s.length()][s.length()];
- for(int i=s.length()-1;i>=0;i--){
- for(int j=i;j<s.length();j++){
- if(s.charAt(i)==s.charAt(j)&&(j-i<=2||plain[i+1][j-1])){
- plain[i][j]=true;
- if(maxLen<j-i+1){
- maxLen = j-i+1;
- start = i;
- end = j;
- }
- }
- }
- }
- return s.substring(start, end+1);
- }
1. Dynamic Programming
Let s be the input string, i and j are two indices of the string. Define a 2-dimension array "table" and let table[i][j] denote whether a substring from i to j is palindrome.
Changing condition:
table[i+1][j-1] == 1 && s.charAt(i) == s.charAt(j) => table[i][j] == 1
Time O(n^2) Space O(n^2)
- public String longestPalindrome(String s) {
- if(s = null ||s.length()<=1){
- return s;
- }
- int len = s.length;
- int maxLen = 1;
- boolean[][] dp = new boolean[len][len];
- String longest = null;
- for(int l=0;l<s.length()-1;l++){
- for(int i=0;i<len-1;i++){
- j=i+1;
- if(s.charAt(i)==s.charAt(j)&&(j-i<=2||dp[i+1][j-1])){
- dp[i][j]=true;
- if(maxLen<j-i+1){
- maxLen = j-i+1;
- longest = s.substring(i, j+1);
- }
- }
- }
- }
- return longest;
- }
For example, if the input string is "dabcba", the final matrix would be the following:
1 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1
From the table, we can clearly see that the longest string is in cell table[1][5].
第二种方法“是对于每个子串的中心(可以是一个字符,或者是两个字符的间隙,比如串abc,中心可以是a,b,c,或者是ab的间隙,bc的间隙,例如aba是回文,abba也是回文,这两种情况要分情况考虑)往两边同时进 行扫描,直到不是回文串为止。假设字符串的长度为n,那么中心的个数为2*n-1(字符作为中心有n个,间隙有n-1个)。对于每个中心往两边扫描的复杂 度为O(n),所以时间复杂度为O((2*n-1)*n)=O(n^2),空间复杂度为O(1)。”引自Code ganker(http://codeganker.blogspot.com/2014/02/longest-palindromic-substring-leetcode.html)
代码如下:
2 if (s.isEmpty()||s== null||s.length() == 1)
3 return s;
4
5 String longest = s.substring(0, 1);
6 for ( int i = 0; i < s.length(); i++) {
7 // get longest palindrome with center of i
8 String tmp = helper(s, i, i);
9
10 if (tmp.length() > longest.length())
11 longest = tmp;
12
13 // get longest palindrome with center of i, i+1
14 tmp = helper(s, i, i + 1);
15 if (tmp.length() > longest.length())
16 longest = tmp;
17 }
18
19 return longest;
20 }
21
22 // Given a center, either one letter or two letter,
23 // Find longest palindrome
24 public String helper(String s, int begin, int end) {
25 while (begin >= 0 && end <= s.length() - 1 && s.charAt(begin) == s.charAt(end)) {
26 begin--;
27 end++;
28 }
29 return s.substring(begin + 1, end);
30 }
Reference: