给定一个字符串 s ,请计算这个字符串中有多少个回文子字符串。
具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被视作不同的子串。
示例 1:
输入:s = “abc”
输出:3
解释:三个回文子串: “a”, “b”, “c”
示例 2:
输入:s = “aaa”
输出:6
解释:6个回文子串: “a”, “a”, “a”, “aa”, “aa”, “aaa”
提示:
1 <= s.length <= 1000
s 由小写英文字母组成
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/a7VOhD
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
本题:字符串的下标为 i 第 i 个字符本身可以成为长度为奇数的回文子字符串的对称中心,同时 i 和 i + 1 可以成为长度为偶数的对称中心,本题是从中间往两侧遍历,每次 for 循环调用两次 countParam 方法,计算奇数和偶数的回文子串个数。
class Solution {
public int countSubstrings(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int count = 0;
for (int i = 0; i < s.length(); i++) {
count += countParam(s, i, i);
count += countParam(s, i, i + 1);
}
return count;
}
public int countParam(String s, int start, int end) {
int count = 0;
while (start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)){
count++;
start--;
end++;
}
return count;
}
}