Palindromic Substrings

Problem Id: 647 Difficulty: Medium


Intuition

Solution


class Solution:
    def countSubstrings(self, s: str) -> int:
        ans = 0
        s = '.' + '.'.join(s) + '.'
        for i in range(len(s)):
            l = 1
            while i - l >= 0 and i + l < len(s) and s[i - l] == s[i + l]:
                l += 1
            ans += l // 2
        return ans