2018-12-03 leetcode Palindromic Substrings Palindromic SubstringsOne line Version - 924ms 1234567class Solution: def countSubstrings(self, s): """ :type s: str :rtype: int """ return sum(s[i:j] == s[i:j][::-1] for j in range(len(s) + 1) for i in range(j)) O(n) Version - 128ms 12345678910111213141516class Solution: def countSubstrings(self, S): """ :type s: str :rtype: int """ N = len(S) ans = 0 for center in range(2*N - 1): left = center // 2 right = left + center % 2 while left >= 0 and right < N and S[left] == S[right]: ans += 1 left -= 1 right += 1 return ans test code 12In [25]: s = Solution(); t = s.countSubstrings('abc'); print(t)3 leet code Newer Find All Numbers Disappeared in an Array Older Queue Reconstruction by Height