Check the max expansion for each odd and even index, and sum then up.
Code
class Solution {
public:
int countSubstrings(string s) {
int n = s.size();
int totalCnt = 0;
for(int i = 0 ; i < n ; ++i) {
totalCnt += expand(s, i, i);
totalCnt += expand(s, i, i+1);
}
return totalCnt;
}
int expand(const string& s, int l, int r) {
int n = s.size();
int cnt = 0;
while(l >= 0 && r <= n-1 && s[l] == s[r]) {
--l;
++r;
++cnt;
}
return cnt;
}
};