647 - Palindromic Substrings
Written on January 21, 2020
Tweet
Given a string, your task is to count how many palindromic substrings in this string. The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.
class Solution:
def countSubstrings(self, s: str) -> int:
if not s:
return 0
def count(left, right):
count = 0
while left >= 0 and right < len(s) and s[left] == s[right]:
count += 1
left -= 1
right += 1
return count
counts = [count(i, i) + count(i, i + 1) for i in range(len(s))]
return sum(counts)