题目:
解析:
注意:字串和子数组差不多
状态表示:
状态转移方程:
初始化:
填表顺序:
返回值:
返回dp表里true的个数
代码:
public int countSubstrings(String ss) {
char[] s = ss.toCharArray();
int n = s.length;
boolean[][] dp = new boolean[n][n];
int count = 0;
for(int i = n-1; i >= 0; i--){
for(int j = i; j < n ; j++){
/**
if(i == j) dp[i][j] = true;//同一个位置
else if(i+1 == j) dp[i][j] = true;//相邻位置
else dp[i][j] = dp[i+1][j-1];//i和j之间找
*/
if(s[i] == s[j])
dp[i][j] = i+1 < j ? dp[i+1][j-1] : true;
if(dp[i][j] == true) count++;
}
}
return count;
}