```
def countSubstrings(self, s):
n = len(s)
def extend(i, j):
count = 0
while i >= 0 and j < n and s[i] == s[j]:
count += 1
i -= 1
j += 1
return count
return sum(extend(i, i) + extend(i, i + 1) for i in range(n))
```