Title link: LeeCode647 palindrome substring
Title description:
From the title we can see that a single character operator string can be repeated, so the string title is calculated as the string length plus the actual number of palindrome substrings. Draw a table. When two characters are equal and similar If the minus length is less than two, it must be a palindrome. When the characters are equal and i+1 to j-1 are still palindrome, it is another palindrome.
class Solution {
public int countSubstrings(String s) {
int[][] dp=new int[s.length()][s.length()];
int ans=0;
for (int j = 0; j < s.length(); j++) {
for (int i = 0; i < j; i++) {
if(s.charAt(i)==s.charAt(j)){
if(j-i<=2){
dp[i][j]=1;
ans+=1;
}
if(dp[i+1][j-1]!=0){
dp[i][j]=1;
ans+=1;
}
}
}
}
return s.length()+ans;
}
}