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 32
| class Solution { public int countSubstrings(String s) { int n = s.length(); int[] dp = new int[n+1]; dp[1] = 1;
for (int i = 2; i <= n; i++) { dp[i] = dp[i - 1] + 1; for (int j = i - 1; j > 0; j--) { String str = s.substring(j - 1, i); if (judge(str)) { dp[i] += 1; } } } return dp[n]; }
public boolean judge(String s) { int i = 0; int j = s.length() - 1; while (i < j) { if (s.charAt(i) != s.charAt(j)) { return false; } i++; j--; } return true; } }
|