string 有多少palindrome substring。
exp: 'aba' 返回4 , 'abba' 返回6
private int getNumOfPalinSubStr(String str) {
if (str == null || str.length() == 0) {
return 0;
}
int count = 0;
for (int i = 0; i < str.length(); i++) {
int start = i;
int end = i;
while (start >= 0 && end < str.length() && str.charAt(start) == str.charAt(end)) {
count++;
start--;
end++;
}
start = i - 1;
end = i;
while (start >= 0 && end < str.length() && str.charAt(start) == str.charAt(end)) {
count++;
start--;
end++;
}
}
return count;
}
没有评论:
发表评论