[longest palindrome substring of leetcode Hot 100]


The first question of new year’s brush is as follows:

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


Example 1:

Enter: S = “bad”
Output: “bab”
Explanation: “ABA” is also the answer to the question.
Example 2:

Enter: S = “CBBD”
Output: “BB”
Example 3:

Input: S = “a”
Output: “a”
Example 4:

Enter: S = “AC”
Output: “a”


1 <= s.length <= 1000
S consists only of numbers and English letters (uppercase and / or lowercase)

Source: leetcode
Link: https://leetcode-cn.com/problems/longest-palindromic-substring
The copyright belongs to Lingkou network. Commercial reprint please contact the official authorization, non-commercial reprint please indicate the source.


Initial idea:

The initial idea is to expand each character to the left and right, cycle all the characters, and finally find the longest palindrome string.But I feel that the time complexity of this method is too high, so I don’t think about it. Alas, I’m not confident.

The answer is as follows:

The answer is very detailed. There are three methods. The third one is not mentioned. It’s a bit complicated. The first one is the center expansion algorithm, which is similar to the original sub path


class Solution {
    pair getLongest(string & s,int left,int right)
    {/ * note that this is the while loop, which will continue to expand outward until the longest palindrome string corresponding to the current character is found. */
        While (left > = 0 & & right / * I'm not familiar with the usage of pair, so I haven't thought of using it like this*/
        return {left+1,right-1};
    string longestPalindrome(string s) {
        Int start = 0, end = 0; / * here, the for loop iterates through each character*/
        For (int i = 0; I / * I feel very confused. Why do I call getlong function twice? I simply understand it as two cases: odd length palindrome string and even length palindrome string. */
            auto [left1,right1]=getLongest(s,i,i);
            auto [left2,right2]=getLongest(s,i,i+1);
        }/ * returns the longest palindrome string. The answer is not unique*/
        return s.substr(start,end-start+1);


The second method introduced in the answer is dynamic programming. Look at the code directly

class Solution {
    string longestPalindrome(string s) {
        Vector < vector < int > > DP (n, vector < int > (n)); string strreturn =; / * from length 1 to s.size (), be careful not to be misled by 0*/
        For (int len = 0; len / * characters of each length are traversed and assigned*/
            For (int, I = 0; I / * corresponds to the character itself*/
                }/ * length 1*/
                else if(len==1)
                }/ * others*/
        return strReturn;


The time and space complexity of the two algorithms are compared as follows

Time complexity and space complexity

Center expansion algorithm:O(n*n)                  O(1)

Dynamic planning:O(n*n)     O(n*n)

Recommended Today

Case study of stack SQL optimization: implicit conversion

Data stack is a cloud native station data platform PAAS. We have an interesting open source project on GitHub and gitee: flinkx. Flinkx is a unified data synchronization tool based on Flink, which can collect both static data and real-time data. It is a global, heterogeneous, batch flow integrated data synchronization engine. If you like, […]