返回字符串中回文子串的数目

const countSubstrings = (s) => {
    let len = s.length
    if([0,1].includes(len)) return len
    let res = 0
    const getStr = (s, l, r) => {
        while(l >= 0 && r <= s.length && s[l] === s[r]){
            res++
            l--;
            r++
        }
    }
    for(let i = 0; i < s.length; i++){
        getStr(s, i, i)
        getStr(s, i, i + 1)
    }
    return res
};

  

posted @ 2023-02-01 14:06  671_MrSix  阅读(10)  评论(0编辑  收藏  举报