最长回文子串

来源:互联网 发布:马赛克视频还原软件 编辑:程序博客网 时间:2024/06/05 06:21

Finding the longest palindromic substring is a classic problem of coding interview. This post summarizes 3 different solutions for this problem.

  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(); l++){    for(int i=0; i<len-l; i++){        int j = i+l;        if(s.charAt(i)==s.charAt(j) && (j-i<=2||dp[i+1][j-1])){            dp[i][j]=true;            if(j-i+1>maxLen){               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].

  1. A Simple Algorithm

Time O(n^2), Space O(1)

public String longestPalindrome(String s) {
if (s.isEmpty()) {
return null;
}

if (s.length() == 1) {    return s;}String longest = s.substring(0, 1);for (int i = 0; i < s.length(); i++) {    // get longest palindrome with center of i    String tmp = helper(s, i, i);    if (tmp.length() > longest.length()) {        longest = tmp;    }    // get longest palindrome with center of i, i+1    tmp = helper(s, i, i + 1);    if (tmp.length() > longest.length()) {        longest = tmp;    }}return longest;

}

// Given a center, either one letter or two letter,
// Find longest palindrome
public String helper(String s, int begin, int end) {
while (begin >= 0 && end <= s.length() - 1 && s.charAt(begin) == s.charAt(end)) {
begin–;
end++;
}
return s.substring(begin + 1, end);
}
3. Manacher’s Algorithm

Manacher’s algorithm is much more complicated to figure out, even though it will bring benefit of time complexity of O(n). Since it is not typical, there is no need to waste time on that.

0 0