blob: 8de80a0a5bcf5660358f2a91b6e660220ea0d9b8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#include "leetcode.h"
class Solution {
public:
int countSubstrings(string s) {
this->s = s;
int ans = 0, n = s.size();
dp.resize(n, vector<int>(n, -1));
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++)
ans += helper(i, j, s);
return ans;
}
private:
vector<vector<int>> dp;
// string s = "abc";
int helper(int l, int r, string s) {
if (l > r || l == r)
return 1;
if (dp[l][r] != -1)
return dp[l][r];
if (s[l] != s[r])
return dp[l][r] = 0;
if (helper(l + 1, r - 1, s))
return dp[l][r] = 1;
return dp[l][r] = 0;
}
};
int main() {}
|