class Solution {
private: int start=0;
int maxlen=0;
public:
string longestPalindrome(string s) {
int len = s.size();
if(len<2)
return s;
for(int i=0;i<len-1;++i){
extendpalin(s,i,i);
extendpalin(s,i,i+1);
}
return s.substr(start,maxlen);
}
private:
void extendpalin(string s,int j,int k){
while(j>=0&&k<s.size()&&s[j]==s[k]){
--j;
++k;
}
if(maxlen<(k-j-1)){
maxlen = k-j-1;
start = j+1;
}
}
};
private: int start=0;
int maxlen=0;
public:
string longestPalindrome(string s) {
int len = s.size();
if(len<2)
return s;
for(int i=0;i<len-1;++i){
extendpalin(s,i,i);
extendpalin(s,i,i+1);
}
return s.substr(start,maxlen);
}
private:
void extendpalin(string s,int j,int k){
while(j>=0&&k<s.size()&&s[j]==s[k]){
--j;
++k;
}
if(maxlen<(k-j-1)){
maxlen = k-j-1;
start = j+1;
}
}
};