Longest Palindromic Substring

题目

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.

思路一:最原始的 Brute force solution, O(N3) time

这里分两种情况,中心元素有一个的或有两个的。

只能在 Juge Small 里才能通过。

而且代码写的实在太难看,太糟糕了!改进的代码在后面。

class Solution {
public:
    string longestPalindrome(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int len = s.length();
        string res = "";
        if(len<=1)
            return s;
        int maxlen = 1;
        string result = "";
        for(int i=0;i<len;i++)
        {
            int tmp = 1;
            while(i-tmp>=0 && i+tmp<len)
            {
                res = s.substr(i-tmp,2*tmp+1);
                if(!ispalin(res))
                {
                    res = res.substr(1,2*tmp-1);
                    break ;
                }
                tmp++;         
            }
            if(2*tmp-1>maxlen)
            {
                maxlen = 2*tmp-1;
                result = res;
            }             
        }  
        for(int i=1;i<len;i++)
        {
            int tmp = 1;
            while(i-tmp>=0 && i+tmp-1<len)
            {
                res = s.substr(i-tmp,2*tmp);
                if(!ispalin(res))
                { 
                    res = res.substr(1,2*tmp-2);
                    break ;
                }
                tmp++;         
            }
            if(2*tmp-2>maxlen)
            {
                maxlen = 2*tmp-2;
                result = res;
            }            
        }        
        return result;        
    }
    bool ispalin(string s)
    {
        if(s.length()<=0)
            return false;
        int i=0;
        int j=s.length()-1;
        while(i<=j)
        {
            if(s[i]!=s[j])
                return false;
            i++;
            j--;
        }
        return true;
    }    
};


思路二:逆序字符串,求取最长公共子串

这里会有一个误区,求S 和 S' 的LCS并不一定是最长回文,因为如果S中含有一个子字符串(非回文)的逆序,那么该方法就会返回该字符串,结果错误。

所以在得到 LCS 后还需要判断它是不是回文,如果不是,再找下一个LCS。

Reverse S and become S’. Find the  longest common substring between S and S’, which must also be the longest palindromic substring.

This seemed to work, let’s see some examples below.

For example,
S = “caba”, S’ = “abac”.
The longest common substring between S and S’ is “aba”, which is the answer.

Let’s try another example:
S = “abacdfgdcaba”, S’ = “abacdgfdcaba”.
The longest common substring between S and S’ is “abacd”. Clearly, this is not a valid palindrome.

We could see that the longest common substring method fails when there exists a reversed copy of a non-palindromic substring in some other part of S. To rectify this, each time we find a longest common substring candidate, we check if the substring’s indices are the same as the reversed substring’s original indices. If it is, then we attempt to update the longest palindrome found so far; if not, we skip this and find the next candidate.

参考 :最大子序列、最长递增子序列、最长公共子串、最长公共子序列、字符串编辑距离  和  LCS 


思路三:动态规划 Dynamic programming solution, O(N2) time and O(N2) space

Consider the case “ababa”. If we already knew that “bab” is a palindrome, it is obvious that “ababa” must be a palindrome since the two left and right end letters are the same.

Stated more formally below:

Define P[ i, j ] ← true  iff the substring S i … S j is a palindrome, otherwise false.

Therefore,

P[ i, j ] ← ( P[ i+1, j-1 ]  and S i = S j )

The base cases are:

P[ i, i ] ← true
P[ i, i+1 ] ← ( S i = S i+1 )

This yields a straight forward DP solution, which we first initialize the one and two letters palindromes, and work our way up finding all three letters palindromes, and so on… 

This gives us a run time complexity of O(N2) and uses O(N2) space to store the table.

class Solution {
public:
    string longestPalindrome(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int len = s.size();
        bool table[1000][1000] = {false};
        int begin = 0;
        int maxlen = 1;
        for(int i=0;i<len;i++)
            table[i][i] = true;
        for(int i=1;i<len;i++)
            if(s[i-1]==s[i])
            {
                table[i-1][i] = true;
                begin = i-1;
                maxlen = 2;
            }
        for(int le=3;le<=len;le++)
        {
            for(int i=0;i<len-le+1;i++)
            {
                int j=i+le-1;
                if(s[i]==s[j] && table[i+1][j-1])
                {
                    table[i][j] = true;
                    begin = i;
                    maxlen = le;                 
                }
            }
        }
        return s.substr(begin,maxlen);          
    }
};


但是上述动态规划的空间消耗实在是太大了。

思路四:中心逐渐扩展,O(N2) time and O(1) space

方法类似于 思路一 ,但是不是 Brute force 的全部遍历。

We observe that a palindrome mirrors around its center. Therefore, a palindrome can be expanded from its center, and there are only 2N-1 such centers.

You might be asking why there are 2N-1 but not N centers? The reason is the center of a palindrome can be in between two letters. Such palindromes have even number of letters (such as “abba”) and its center are between the two ‘b’s.

Since expanding a palindrome around its center could take O(N) time, the overall complexity is O(N2).

总共有 2*len-1个 扩展中心,类似于 思路一 中的 两个for循环。

class Solution {  
public:  
    string longestPalindrome(string s) {  
        // Start typing your C/C++ solution below  
        // DO NOT write int main() function  
        int len = s.length();  
        if(len<=1)
            return s;
        string result = "";  
        for(int i=0;i<len-1;i++)  
        {  
            string t1 = expandString(s,i,i);  
            if(t1.length()>result.length())  
                result = t1;  
            string t2 = expandString(s,i,i+1);  
            if(t2.length()>result.length())  
                result = t2;  
        }  
        return result;    
    }  
    string expandString(string &s, int l, int r)  
    {  
        while(l>=0 && r<=s.size()-1 && s[l]==s[r])  
        {  
            l--;  
            r++;  
        }  
        return s.substr(l+1,r-l-1);    // r-1-(l+1)+1=r-l+1  
    }      
};  

其他参考   http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值