题解 | #密码截取#

密码截取

https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        int len = s.length();
        int maxL = 0;
        for (int i = 0; i < len; i++) {
            int left = i - 1;
            int right = i + 1;
            char ch = s.charAt(i);
            int subMaxLen = 1;
            while (left >= 0 && s.charAt(left) == ch) {
                left--;
                subMaxLen++;
            }
            while (right < len && s.charAt(right) == ch) {
                right++;
                subMaxLen++;
            }
            while (left >= 0 && right < len && s.charAt(left) == s.charAt(right)) {
                left--;
                right++;
                subMaxLen += 2;
            }
            maxL = Math.max(maxL, subMaxLen);
        }
        System.out.println(maxL);
    }

}

全部评论

相关推荐

2024-12-09 12:21
门头沟学院 C++
l11hy:今早刚开,已满足
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务