647. Palindromic Substrings

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

solution

  • 动态规划

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

  • 双指针

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

    def traversal(self, s, l, r):
        this_res = 0

        while l >= 0 and r < len(s) and s[l] == s[r]:
            this_res += 1
            l -= 1
            r += 1
        return this_res

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

Last updated