647.回文子串 -点击查看题目要求
解题思路:从中心向两侧延伸
字符串的回文子串的中心位置有2N个(从0-2N-1)
根据中心位置的不同,从最小的回文子串慢慢向外延伸
class Solution {
public int countSubstrings(String s) {
int N = 2*s.length()-1;
int num = 0;
for(int center = 0; center < N; center++) {
int left = center/2;
int right = left+center%2;
while(left>=0&&right<s.length()&&s.charAt(left)==s.charAt(right)) {
num++;
left--;
right++;
}
}
return num;
}
}