5. Longest Palindromic Substring [palindromic longest substring]

description

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000

Given a string s, s to find the longest substring palindromic. You may assume that s the maximum length of 1000

Examples of
Here Insert Picture Description
ideas

  • Method 1 tornado method, find longan

Longan two kinds: one kind bab, one kind baab, i.e. (i, i), and (i, i + 1)

  • Method 2 Dynamic Programming
    answer
  • python
*方法1*
class Solution:
    def check(self, s:string, a:int,b:int)->str:
        while a>=0 and b<len(s) and s[a]==s[b]:
            a-=1
            b+=1
        a+=1
        b-=1
        return s[a:b+1]

    def longestPalindrome(self, s: str) -> str:
        res = ""
        for i in range(len(s)):
            
            s2 = self.check(s,i,i)
            s3 = self.check(s,i,i+1)
            if len(res)<len(s2):
                res = s2
            if len(res)<len(s3):
                res = s3
        return res
*方法2*
    def longestPalindrome(self, s: str) -> str:
        res = ""
        dp=[[0]*len(s) for i in range(len(s))]
        for r in range(len(s)):#r=j-i 0~len-1 r为0时是一个元素,为1时为两个元素在一起
            for i in range(len(s)-r): #i+r<=len-1(最大下标)
                j=i+r
                if i==j:
                    dp[i][j]=1
                if i+1==j:
                    dp[i][j]=1 if s[i]==s[j] else 0
                   
                if i+2<=j:
                    dp[i][j]=dp[i+1][j-1] and s[i]==s[j]
                if dp[i][j] and j-i+1>len(res):
                    res=s[i:j+1]
       
  • c++
*方法1*
class Solution {
private:
    string check(string s, int a, int b){
        while (a>=0 && b<=s.size() && s[a]==s[b])
        {
            a--;
            b++;
        }
        a++;
        b--;
        return s.substr(a,b-a+1);
    }
public:
    
    string longestPalindrome(string s) {
        string res("");
        for (int i=0; i<s.size(); i++)
        {
            string s2 = check(s,i,i);
            string s3 = check(s,i,i+1);
            if (res.size()<s2.size())
                res=s2;
            if (res.size()<s3.size())
                res=s3;
            
        }
        return res;
    }
    
};
*方法2*
class Solution {
public:
    string longestPalindrome(string s) {
        int L = s.size();
        string res("");
        vector<vector<int>> dp(L,vector<int>(L));//全是0的二维数组
        for(int r=0; r<L; r++)
            for (int i=0; i<L-r; i++)
            {
                int j=i+r;
                if (i==j)
                    dp[i][j]=1;
                if (i+1==j)
                    dp[i][j]=s[i]==s[j]?1:0;
                if (i+2<=j)
                    dp[i][j]=(dp[i+1][j-1] && s[i]==s[j])?1:0;
                if (dp[i][j] && j-i+1>res.size())
                    res = s.substr(i,j-i+1);
                
            }
        
        return res;
    }
};
Published 78 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/puspos/article/details/102940845