647. Palindromic Substrings

https://leetcode.com/problems/palindromic-substrings/

solution

  • 双指针: 中心扩散

class Solution:
    def countSubstrings(self, s: str) -> int:
        res = 0
        for i in range(len(s)):
            res += self.palindromic(s, i, i)
            res += self.palindromic(s, i, i+1)
        return res

    def palindromic(self, s, i, j):
        temp_res = 0
        while i >=0 and j < len(s) and s[i] == s[j]:
            temp_res += 1
            i -= 1
            j += 1
        return temp_res

时间复杂度:O() 空间复杂度:O()

时间复杂度:O() 空间复杂度:O()

follow up

回文类

Last updated