题解 | #密码截取#
密码截取
http://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.next();
int num = getLength(str);
System.out.println(num);
}
}
private static int getLength(String str) {
int res = 0;
for (int i = 0; i < str.length(); i++) {
//ABA型
int len1 = getStrL(str, i, i);
//ABBA型
int len2 = getStrL(str, i, i + 1);
if(len1 >= len2){
res = Math.max(res, len1);
}else{
res = Math.max(res, len2);
}
}
return res;
}
private static int getStrL(String str, int i, int l) {
int len = 0;
while (i >= 0 && l < str.length() && str.charAt(i) == str.charAt(l)) {
i--;
l++;
}
return l - i - 1;
}
}