//回文分为两种,奇数和偶数情况,遍历求出x为中心的两种回文的最长字串,取最大值即可
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param A string字符串
* @return int整型
*/
public int getLongestPalindrome (String A) {
if (A == null || A.length() == 0) {
return 0;
}
// wrAite code here
int n = A.length();
int res = 1;
int res2 = 0;
for (int i = 1; i < n - 1 - (res / 2); i++) {
res = Math.max(res, getLongestPalindrome(A, n, i));
}
for (int i = 0; i < n - 1 - (res / 2); i++) {
res2 = Math.max(res2, getLongestPalindromeD(A, n, i));
}
return Math.max(res, res2);
}
public int getLongestPalindrome (String A, int n, int index) {
int longest = Math.min(index, (n - index - 1));
int res = 1;
for (int i = 1; i <= longest; i++) {
if (A.charAt(index - i) != A.charAt(index + i)) {
break;
}
res += 2;
}
return res;
}
public int getLongestPalindromeD (String A, int n, int index) {
int longest = Math.min(index, (n - index - 2));
int res = 0;
for (int i = 0; i <= longest; i++) {
if (A.charAt(index - i) != A.charAt(index + 1 + i)) {
break;
}
res += 2;
}
return res;
}
}