题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.on('line', function (line) { let max = 1 for (let i = 0 ; i < line.length; i++){ let temp1 = findMax(line,i, i) let temp2 = line.charAt(i)===line.charAt(i+1)?findMax(line, i, i+1):1 max = Math.max(max, temp1, temp2) } console.log(max) }); function findMax(str:string, left:number, right: number){ let max = 1 while (left >= 0 && right < str.length){ if (str.charAt(left)===str.charAt(right)){ max = Math.max(max, right-left+1) left -= 1 right += 1 }else{ break } } return max }
双指针。
一个字符串是不是回文串如何判断。
单个字符串必然是回文串,之后向两边扩散就好