题解 | #最长回文子串#
最长回文子串
http://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
class HelloWorld {
public static void main(String[] args) {
//双层循环 截所有的串 判断串是不是回文串 记录长度取最大
Scanner in = new Scanner(System.in);
String s = in.nextLine();
int len = s.length();
int res = 0;
//System.out.println(isHw("abba"));
for (int i = 0; i < len; i++) {
for (int j = i + 1; j <= len; j++) {
String sb = s.substring(i, j);
System.out.println(sb);
if (isHw(sb)) {
res = Math.max(res,sb.length());
}
}
}
System.out.println(res);
}
static boolean isHw(String ss) {
int s = 0, end = ss.length() - 1;
while (s <= end) {
if (ss.charAt(s) != ss.charAt(end)) {
return false;
}
s++;
end--;
}
return true;
}
}