Palindromic Substrings
Input: "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".Input: "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".Input: "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".Input: "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa". class Solution(object):
def countSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
# 以left和right为初始位置,向两边扩张,每次得到新回文序列,则count++
def expand(left, right):
while left <= right and left >= 0 and right < len(s) and s[left] == s[right]:
self.count += 1
left -= 1
right += 1
self.count = 0
for i in range(len(s)):
expand(i, i)
expand(i, i + 1)
return self.count