题解 | #密码截取#

密码截取

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;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
06-24 14:18
点赞 评论 收藏
分享
头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务