5. Longest Palindromic Substring 找出给定字符串中最长的子字符串

来源:互联网 发布:过亿收入的淘宝店铺 编辑:程序博客网 时间:2024/06/04 20:15

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

转载出处:http://www.programcreek.com/2013/12/leetcode-solution-of-longest-palindromic-substring-java/

算法的做法是:是对于每个子串的中心(可以是一个字符,或者是两个字符的间隙,比如串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)。

public class Solution {
    public String longestPalindrome(String s) {
        int len=s.length();
        if(s==null || len<2) return s;
        String longest=s.substring(0,0);  //记得定义的时候要初始化哦,不然报错,我这儿相当于是定义为了空串
        for(int i=0;i<len;++i){
            // get longest palindrome with center of i
            String temp=getSubstring(s,i,i);
            if(temp.length()>longest.length())
                longest=temp;
            // get longest palindrome with center of i, i+1
            temp=getSubstring(s,i,i+1);
            if(temp.length()>longest.length())
                longest=temp;
        }
        return longest;
    }
    // Given a center, either one letter or two letter, 
    // Find longest palindrome
    private String getSubstring(String s,int begin,int end){
        int len=s.length();
        while(begin>=0 && end<=len-1 && s.charAt(begin)==s.charAt(end)){
            --begin;
            ++end;
        }
        return s.substring(begin+1,end);  //s,substring(i,j)中的关系为[i,j),如果i=j,则为空串,子串的长度为j-i,如果i>j,则会抛异常,这一句好不错,s.substring(begin+1,end),恰好返回了当前条件下边的最长的那个字符串,同时涵盖了上边两种情况(即字符数为奇数个或者偶数个)
    }
}


2. 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.

Start condition:

table[i][i] == 1;table[i][i+1] == 1  => s.charAt(i) == s.charAt(i+1) 

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 class Solution {
    public String longestPalindrome(String s) {
        int len=s.length(); 
        if (s == null || len<2) return s;
 
        int maxLen = 1;
        String longestStr = s.substring(0,0);
        int[][] flag = new int[len][len];
 
        //every single letter is palindrome
        for (int i = 0; i < len; ++i)
            flag[i][i] = 1;
   
        //condition for calculate whole flag
        for (int k = 2; k <= len; ++k) { //k为子串的最大长度,注意最长可以达到len,即k为j-i+1
            for (int i = 0; i <= len-k; ++i) {
                int j = i + k - 1;
                if (s.charAt(i) == s.charAt(j)) {
                    flag[i][j] = flag[i + 1][j - 1];
                    if (flag[i][j] == 1 && k > maxLen){
                        longestStr = s.substring(i, j + 1);
                        maxLen=k;
                    }
                }         
                 else 
                    flag[i][j] = 0;
            }
        }
        return longestStr;
    }
}

0 0
原创粉丝点击