Leetcode problem solution 5-the longest palindrome substring

Problem Description

Give you a string s and find the longest palindrome substring in s.

Example 1:

输入:s = "babad"
输出:"bab"
解释:"aba" 同样是符合题意的答案。

Example 2:

输入:s = "cbbd"
输出:"bb"

Example 3:

输入:s = "a"
输出:"a"

Example 4:

输入:s = "ac"
输出:"a"
 

prompt:

1 <= s.length <= 1000
s 仅由数字和英文字母(大写和/或小写)组成

Problem-solving ideas

For a substring, if it is a palindrome and the length is greater than 2, then after removing the first and last two letters, it is still a palindrome. For example, for the string {"ababa"}, if we already know that {"bab"} is a palindrome string, then {"ababa''} must be a palindrome string, because its first and last two letters are both " a".
According to this idea, we can use dynamic programming to solve this problem.
If the two characters at the beginning and the end of a string are not equal, then the string must not be a palindrome;
if the two characters at the beginning and the end of a string are equal, then it is necessary to continue the judgment.

  • If the substring inside is a palindrome, the whole is a palindrome;
  • If the substring inside is not a palindrome, the whole is not a palindrome.

That is: in the case where the head and tail characters are equal, the palindrome property of the substring in it is determined by the palindrome property of the entire substring, which is the state transition. Therefore, the "state" can be defined as whether a substring of the original string is a palindrome substring.
Step 1: Define the state
dp[i][j] to indicate whether the substring s[i…j] is a palindrome substring, where the substring s[i…j] is defined as a left-closed and right-closed interval, which can be taken as s[ i] and s[j].

Step 2: Think about the state transition equation.
In this step, classify and discuss (according to whether the first and last characters are equal), according to the above analysis:

dp[i][i] = (s[i]==s[j])   abd dp[i][j]				当j-i<=1时
dp[i][j] = (s[i] == s[j]) and dp[i + 1][j - 1]      当j-i>1

Implementation code

class Solution {
    
    
    public String longestPalindrome(String s) {
    
    
        int n=s.length();
        char [] arrStr=s.toCharArray();     //将字符串转化为数组,便于运算
        int maxi=0,maxj=0;          //标记最长回文子串的下标
        int maxlen=0;
        //标记数组,标记从起点i到终点j之间的字符是否是回文子串,默认全为false
        boolean [][] f=new boolean[n][n];      
        //这里i是终点,j是i之前的字符,即以i为终点,遍历i之前的字符是否为回文子串
        //注意这里的顺序,一定要保证后面的回文子串利用到前面的回文子串时,前面的回文子串一定被计算出来过了。
        for(int i=0;i<n;i++){
    
    
            for(int j=0;j<=i;j++){
    
    
                //当i-j<=1时,判读那两个字符是否相等,相等即为回文子串,这里包含了i与j重合的情况
                if(i-j<=1){
    
    
                    if(arrStr[i]!=arrStr[j]){
    
    
                        f[j][i]=false;
                    }else{
    
    
                        f[j][i]=true;       //相等时如果大于当前最长回文子串的时候,就记录下来
                        if(maxlen<(i-j+1)){
    
    
                            maxlen=i-j+1;
                            maxi=i;
                            maxj=j;
                        }
                    }
                }else{
    
    
                    //当i-j>1时,需要判断当前两个字符是否相等,并且还要判断子字符串是否为回文子串。
                    if(arrStr[i]==arrStr[j] && f[j+1][i-1]==true){
    
    
                        f[j][i]=true;
                        if(maxlen<(i-j+1)){
    
    
                            maxlen=i-j+1;
                            maxi=i;
                            maxj=j;
                        }
                    }else{
    
    
                        f[j][i]=false;
                    }
                }
            }
        }
        String res="";
        for(int i=maxj;i<=maxi;i++){
    
    
            res+=arrStr[i];
        }
        return res;
    }
}

Guess you like

Origin blog.csdn.net/qq_39736597/article/details/114524888