python - count palindromic substrings - clean


  • 0
    S
    class Solution(object):
        def countSubstrings(self, s):
            """
            :type s: str
            :rtype: int
            """
            
            length = count = len(s)
            if not length:
                return count
            
            for i in range(length-1):
                # even length
                start, end = i, i+1
                while start > -1 and end < length and s[start] == s[end]:
                    start -= 1
                    end += 1
                    count += 1
                
                # odd length
                start, end = i, i+2
                while start > -1 and end < length and s[start] == s[end]:
                    start -= 1
                    end += 1
                    count += 1
                
            
            return count
    

Log in to reply
 

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