题解 | #密码截取#

密码截取

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

相关推荐

06-19 19:06
门头沟学院 Java
码农索隆:别去东软,真学不到东西,真事
点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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