**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”

Tips:

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 {
public:
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);
if(right1-left1>end-start)
{
start=left1;
end=right1;
}
if(right2-left2>end-start)
{
start=left2;
end=right2;
}
}/ * 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 {
public:
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*/
if(len==0)
{
dp[i][i]=1;
}/ * length 1*/
else if(len==1)
{
dp[i][i+len]=s[i]==s[i+len];
}/ * others*/
else
{
dp[i][i+len]=dp[i+1][i+len-1]&&(s[i]==s[i+len]);
}
if(dp[i][i+len]&&len+1>strReturn.size())
{
strReturn=s.substr(i,len+1);
}
}
}
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)**