[LeetCode] 5. 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.

Example:

Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.

Example:

Input: "cbbd"

Output: "bb"

方法

1.

(Dynamic Programming) [Accepted]

To improve over the brute force solution, we first observe how we can avoid unnecessary re-computation while validating palindromes. Consider the case \textrm{''ababa''}”ababa”. If we already knew that \textrm{''bab''}”bab” is a palindrome, it is obvious that \textrm{''ababa''}”ababa” must be a palindrome since the two left and right end letters are the same.

We define P(i,j)P(i,j) as following:

P(i,j)={true,false,if the substring SiSj is a palindromeotherwise.  P(i,j)={true,if the substring Si…Sj is a palindromefalse,otherwise. 

Therefore,

P(i, j) = ( P(i+1, j-1) \text{ and } S_i == S_j )P(i,j)=(P(i+1,j1) and Si==Sj)

The base cases are:

P(i, i) = trueP(i,i)=true

P(i, i+1) = ( S_i == S_{i+1} )P(i,i+1)=(Si==Si+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...

Complexity Analysis

  • Time complexity : O(n^2)O(n2). This gives us a runtime complexity of O(n^2)O(n2).

  • Space complexity : O(n^2)O(n2). It uses O(n^2)O(n2) space to store the table.

Additional Exercise

Could you improve the above space complexity further and how?

代码1(递归编写   结果: Submission Result: Time Limit Exceeded):


class Solution {
public:
    int ispalindromic(string s,int start,int end,int n,int left,int right,string &substring){
        if(left<start){
            substring = s.substr(left+1,n);
            return n;
        }
        if(end<right){
            substring = s.substr(left+1,n);
            return n;
        }
        if(s[left]==s[right]){
            return ispalindromic(s,start,end,n+2,left-1,right+1,substring);
        }
        else{
            substring = s.substr(left+1,n);
            return n;
        }
    }
    
    
public:
    string longestPalindrome(string s) {
        int len = s.length();
        string maxsubstring;
        int Maxlen = -1;
        string tmpstring;
        for(int i = 0;i<len;i++){
            int sublen = ispalindromic(s,0,len-1,1,i-1,i+1,tmpstring);

            if(Maxlen<sublen){
                Maxlen = sublen;
                maxsubstring = tmpstring;
            }
            if(s[i]==s[i+1]){
                int sublen =  ispalindromic(s,0,len-1,2,i-1,i+2,tmpstring);
                if(Maxlen<sublen){
                    Maxlen = sublen;
                    maxsubstring = tmpstring;
                }
                
            }
        }
        return maxsubstring;
    }
};

代码2 (非递归编写,但也运用了把大问题转化为小问题的想法,将多种个情况归为一种处理):

class Solution {
public:
    int ispalindromic(string s,int start,int end,int n,int left,int right,string &substring){
        while(true){
            if(left<start){
                substring = s.substr(left+1,n);
                return n;
            }
            if(end<right){
                substring = s.substr(left+1,n);
                return n;
            }
            if(s[left]==s[right]){
                n+=2;
                left-= 1;
                right+= 1;
            }
            else{
                substring = s.substr(left+1,n);
                return n;
            }
        }
    }
    
    
public:
    string longestPalindrome(string s) {
        int len = s.length();
        string maxsubstring;
        int Maxlen = -1;
        string tmpstring;
        for(int i = 0;i<len;i++){
            int sublen = ispalindromic(s,0,len-1,1,i-1,i+1,tmpstring);

            if(Maxlen<sublen){
                Maxlen = sublen;
                maxsubstring = tmpstring;
            }
            if(s[i]==s[i+1]){
                int sublen =  ispalindromic(s,0,len-1,2,i-1,i+2,tmpstring);
                if(Maxlen<sublen){
                    Maxlen = sublen;
                    maxsubstring = tmpstring;
                }
                
            }
        }
        return maxsubstring;
    }
};

方法

2.

Approach #4 (Expand Around Center) [Accepted]

In fact, we could solve it in O(n^2)O(n2) time using only constant space.

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

You might be asking why there are 2n - 12n1 but not nn centers? The reason is the center of a palindrome can be in between two letters. Such palindromes have even number of letters (such as \textrm{''abba''}”abba”) and its center are between the two \textrm{'b'}’b’s.

Complexity Analysis

  • Time complexity : O(n^2)O(n2). Since expanding a palindrome around its center could take O(n)O(n) time, the overall complexity is O(n^2)O(n2).

  • Space complexity : O(1)O(1).

代码 :

class Solution {
public:
    int expandAroundcenter(string s,int left,int right){
        while(left>=0 && right<s.length() && s[left]==s[right]){
            left--;
            right++;
        }
        return right - left -1;
    }

public:
    string longestPalindrome(string s) {
        int start = 0,end = 0;
        for(int i=0;i<s.length();i++){
            int len1 = expandAroundcenter(s,i,i);
            int len2 = expandAroundcenter(s,i,i+1);
            int len = max(len1,len2);
            if(len>end - start){
                
                start = i-(len -1)/2;
                end = i +len/2;
                end +=1;
                
            }
        }
        cout<<start<<end - start<<endl;
        return s.substr(start,end - start);
    }
};





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
校园失物招领系统管理系统按照操作主体分为管理员和用户。管理员的功能包括字典管理、论坛管理、公告信息管理、失物招领管理、失物认领管理、寻物启示管理、寻物认领管理、用户管理、管理员管理。用户的功能等。该系统采用了Mysql数据库,Java语言,Spring Boot框架等技术进行编程实现。 校园失物招领系统管理系统可以提高校园失物招领系统信息管理问题的解决效率,优化校园失物招领系统信息处理流程,保证校园失物招领系统信息数据的安全,它是一个非常可靠,非常安全的应用程序。 ,管理员权限操作的功能包括管理公告,管理校园失物招领系统信息,包括失物招领管理,培训管理,寻物启事管理,薪资管理等,可以管理公告。 失物招领管理界面,管理员在失物招领管理界面中可以对界面中显示,可以对失物招领信息的失物招领状态进行查看,可以添加新的失物招领信息等。寻物启事管理界面,管理员在寻物启事管理界面中查看寻物启事种类信息,寻物启事描述信息,新增寻物启事信息等。公告管理界面,管理员在公告管理界面中新增公告,可以删除公告。公告类型管理界面,管理员在公告类型管理界面查看公告的工作状态,可以对公告的数据进行导出,可以添加新公告的信息,可以编辑公告信息,删除公告信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值