class Solution {
public:
int countSubstrings(string s) {
// 寻找递推关系
// 一个回文子串的左右两元素如果相等,则也是回文子串
// 换句话说,如果两个元素相等,且中间是回文子串,则必为回文串
// dp数组从左下到右上
int res = 0;
int len = s.length();
vector<vector<bool>> dp =
vector<vector<bool>>(len+1, vector<bool>(len+1, false));
for (int i = len; i >= 1; i--) {
for (int j = 1; j <= len; j++) {
if (i > j)
continue;
if (s[i - 1] == s[j - 1]) {
if (j - i <= 1) {
dp[i][j] = true;
res++;
}
if (i < j - 1) {
dp[i][j] = dp[i + 1][j - 1];
if (dp[i][j] == true)
res++;
}
}
}
}
return res;
}
};