647. Palindromic Substrings——string

647. Palindromic Substrings——string

 

题目分析:以每个字母为中间位置,向外拓展,找到所有的回文序列

class Solution(object):
    def repeatedNTimes(self, s):
        count = 0
        for i in range(len(s)):
            count += 1
            # 回文长度是奇数的情况
            left = i - 1
            right = i + 1
            while left >= 0 and right < len(s) and s[left] == s[right]:
                count += 1
                left -= 1
                right += 1
            # 回文长度是偶数的情况
            left = i
            right = i + 1
            while left >= 0 and right < len(s) and s[left] == s[right]:
                count += 1
                left -= 1
                right += 1
        return count