题解 | #密码截取#
密码截取
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); } }