class Solution {
public String longestPalindrome(String s) {
int start = 0, end = -1;
StringBuilder t = new StringBuilder("#");
for (int i = 0; i < s.length(); ++i) {
t.append(s.charAt(i));
t.append('#');
}
t.append('#');
s = t.toString();
int R=-1,C=-1;//最右能扩边界和中心位置,让R为右边界+1方便计算
int max=0;
int []Rstr=new int[s.length()];//回文半径数组
int left=0,right=0;//子串左边界和右边界
for(int i=0;i<s.length();i++){
Rstr[i]=R>i?Math.min(Rstr[2*C-i],R-i):1;
while(i+Rstr[i]<s.length()&&i-Rstr[i]>=0&&s.charAt(i+Rstr[i])==s.charAt(i-Rstr[i])){
Rstr[i]++;
}
if(Rstr[i]+i>R){
R=Rstr[i]+i;
C=i;
}
if(Rstr[i]>max){
left=i-Rstr[i]+1;
right=i+Rstr[i];
max=Rstr[i];
}
}
StringBuilder str=new StringBuilder();
for(int i=left;i<right;i++){
if(s.charAt(i)!='#')str.append(s.charAt(i));
}
return str.toString();
}
}