题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
import java.util.Scanner fun main(args: Array<String>) { fun max(a:Int, b:Int) = Math.max(a, b) val read = Scanner(System.`in`) val res = read.nextLine() var max = 0 for (i in res.indices) { max = max(max, getLongest(res, i, i)) max = max(max, getLongest(res, i, i + 1)) } println(max) } fun getLongest(res: String, l1: Int, r1: Int): Int { var l = l1 var r = r1 while (l>=0 && r<=res.lastIndex && res[l] == res[r]) { l -- r ++ } return r - l + 1 -2 }