Simple Python Solution


  • 0
    Y
    def countSubstrings(self, s):
        if not s:
            return 0
        
        count = 0
        for i in range(len(s)):
            for j in range(2):
                # count number of palindrome substrings centered at i
                count += self.helper(s, i, i + j)
                
        return count
    
    def helper(self, s, i, j):
        count = 0
        while i >= 0 and j < len(s) and s[i] == s[j]:
            count += 1
            i -= 1
            j += 1
        
        return count

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.