Leetcode longest palindrome substring

Keywords: less

Child leetcode Longest Palindromic Substring

Because I love it.

1005 longest palindrome

The longest palindrome string is a string that reads the same from left to right or from right to left. Obviously, the string is left and right symmetrical, so we can find the large string through the small string.

For example, the two strings, abbcbba and abbbba, take abbcbba as an example. It is obvious that it is left and right symmetrical with c as the axis of symmetry. Therefore, as long as we find the string bcb, and then extend it to the left and right at the same time, we can find all palindrome strings. If we look at the abbbbba string again, it shows that it takes the interval between the second and third bits as the axis of symmetry (counting from 0). So as long as we find BB (on the second and third bits), we can find all palindrome strings by left and right synchronous expansion.

You may wonder why we should give the same two examples for a reason. If we look at it a little bit, we can see that all palindrome strings (I think only the string a in the string ab can't be called a palindrome string) can be made up of 2-bit palindrome strings or 3-bit palindrome strings. This is an obvious conclusion. We also used an example from the expansion of 2-bit return string and two examples from the expansion of 3-bit return string.

With the above conclusion, this problem can be solved easily. As long as we traverse the original string twice, look for the one expanded by 2 for the return string, look for the one expanded by one return string, and find the largest return string.

class Solution {
public:
    bool judge(string s) {
        //Determine whether it is a palindrome string
        int len = s.length()-1;
        if (len == 0) 
            return true;
        for (int i = 0; i < len; ++i) {
            //cout<< s[i]<< " "<< s[len-i]<< "\n";
            if (s[i] == s[len-i]) {
                if (i == len/2 || (i == len/2 -1 && len % 2 == 0)) {
                    //Because the length of the string may be even or odd, the judgment is complex and the logic is not clear
                    //cout<< s;
                    return true;
                }
            } else {
                return false;
            }
        }
        return false;
    }

    string longestPalindrome(string s) {
        string ans = "";
        string temp = "";   //Intermediate string of records
        int maxm = 0;
        int i, j;
        for (i = 0; i < (int)s.length()-1; ++i) {
            // Note here I < s.length() - 1 instead of - 2
            temp = "";
            // Get a two bit substring
            for (j = 0; j < 2; ++j) {
                temp += s[i+j];
            }
            int p = i;
            cout<< "temp: "<< temp<< "\n";
            // To determine whether it is a palindrome string, p q stores the position of the current string head and tail
            if (judge(temp)) {
                int q = i+j-1;
                cout<< p<< " "<< q<< "\n";
                // This comparison is to determine whether the current maxm is 0. If it is less than 2, it is obviously 0,
                // You need to replace ans with the current temp and update the value of maxm
                if (maxm < 2) {
                    maxm = 2;
                    ans = temp;
                }
                // Find a larger palindrome string by expanding left and right at the same time
                while(true) {
                    // Stop searching if the string boundary is exceeded 
                    if (p-1>=0 && q+1 < (int)s.length()){
                        cout<< s[p-1]<< " "<< s[q+1]<< "\n";
                        if (s[p-1] == s[q+1]) {
                            p--;
                            q++;
                            temp = s[p] + temp+s[q];
                            // If you don't find it once, you can update it. If you find it all, it may be faster
                            if (q-p+1 > maxm) {
                                maxm = q-p+1;
                                ans = temp;
                            }
                        } else
                            break;
                    } else {
                        break;
                    }
                }
            }
            cout<< maxm<<"\n";
        }

        // The principle is the same as above, no need to elaborate
        for (i = 0; i < (int)s.length()-2; ++i) {
            temp = "";
            for (j = 0; j < 3; ++j) {
                temp += s[i+j];
            }
            int p = i;
            cout<< "temp: "<< temp<< "\n";
            if (judge(temp)) {
                int q = i+j-1;
                cout<< p<< " "<< q<< "\n";
                if (maxm < 3) {
                    maxm = 3;
                    ans = temp;
                }
                while(true) {
                    if (p-1>=0 && q+1 < (int)s.length()){
                        cout<< s[p-1]<< " "<< s[q+1]<< "\n";
                        if (s[p-1] == s[q+1]) {
                            p--;
                            q++;
                            temp = s[p] + temp+s[q];
                            if (q-p+1 > maxm) {
                                maxm = q-p+1;
                                ans = temp;
                            }
                        } else
                            break;
                    } else {
                        break;
                    }
                }
            }
            cout<< maxm<<"\n";
        }

        if (maxm == 0)
            ans = s[0];
        return ans;
    }
};

Posted by gameshints on Sun, 05 Apr 2020 12:04:34 -0700