const str = 'abc12133321cab' function getRe(str) { const arr = str.split('') let maxLen = 0 for(let i = 0; i <arr.length; i++) { for(let k = i + 1; k < arr.length; k++) { if (arr[i] === arr[k]) { let flag = true for(let n = 0; n < k - n; n++) { if (i + n <= k - n) { if (arr[i + n] !== arr[k - n]) { flag = false break } } } if (flag) { if (maxLen < k - i + 1) { maxLen = k - i + 1 } } } } } return maxLen } console.log(getRe(str))