
cpp
class Solution {
public:
int countSubstrings(string s) {
int N = s.size();
vector<vector<bool>> dp(N,vector<bool>(N,false));
// dp[i][j] = dp[i+1][j-1]
int result = 0;
for(int i=N-1;i>=0;i--){
for(int j=i;j<N;j++){
if(s[i]==s[j]){
if(j-i==0 || j-i==1) dp[i][j]=true;
else dp[i][j] = dp[i+1][j-1];
}
if(dp[i][j]) result++;
}
}
return result;
}
};