// Expand from the center
int countSubstrings(string s) { // time: (n^2); space: O(1)
int n = s.size(), res = 0;
for (int i = 0; i < n; ++i) {
helper(s, i, i, res); // odd length
helper(s, i, i + 1, res); // even length
}
return res;
}
void helper(string& s, int i, int j, int& res) {
while (i >= 0 && j < s.size() && s[i] == s[j]) {
++res;
--i, ++j;
}
}