题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
const str = readline()
const arr = str.split('')
const len = str.length
let left, right
let maxLength = 1
let curLen = 1
for (let i = 1; i < len; i++) {
curLen = 1
left = i - 1
right = i + 1
while (arr[left] === arr[right]) {
curLen += 2
left--
right++
if (left < 0 || right >= len) {
break
}
}
maxLength = curLen > maxLength ? curLen : maxLength
}
//
for (let i = 1; i < len; i++) {
if (arr[i] === arr[i + 1]) {
curLen = 2
left = i - 1
right = i + 2
while (arr[left] === arr[right]) {
curLen += 2
left--
right++
if (left < 0 || right >= len) {
break
}
}
}
maxLength = curLen > maxLength ? curLen : maxLength
}
console.log(maxLength)
const arr = str.split('')
const len = str.length
let left, right
let maxLength = 1
let curLen = 1
for (let i = 1; i < len; i++) {
curLen = 1
left = i - 1
right = i + 1
while (arr[left] === arr[right]) {
curLen += 2
left--
right++
if (left < 0 || right >= len) {
break
}
}
maxLength = curLen > maxLength ? curLen : maxLength
}
//
for (let i = 1; i < len; i++) {
if (arr[i] === arr[i + 1]) {
curLen = 2
left = i - 1
right = i + 2
while (arr[left] === arr[right]) {
curLen += 2
left--
right++
if (left < 0 || right >= len) {
break
}
}
}
maxLength = curLen > maxLength ? curLen : maxLength
}
console.log(maxLength)