给你一个字符串 s,找到 s 中最长的回文子串。
如果字符串的反序与原始字符串相同,则该字符串称为回文字符串。
class Solution {
public:
string longestPalindrome(string s) {
int n = s.length();
vector<vector<bool>> dp(n,vector<bool>(n));
string res;
int begin = 0;
int max_len = 0;
for (int L = 1; L <=n; L++)
{
for (int i = 0; i < n; i++)
{
//L = j+1-i
int j = i+L-1;
if(j>=n){
break;
}
if(s[i]!=s[j]){
dp[i][j] = false;
}else{
dp[i][j] = L<=2 || dp[i+1][j-1];
}
if(dp[i][j] && L>max_len){
begin = i;
max_len = L;
}
}
}
return s.substr(begin,max_len);
}
};