string 有多少palindrome substring。exp: 'aba' 返回4 , 'abba' 返回6
public class CountPalindrome {
public int countPalindrome(String str){
if(str == null || str.length() == 0) return 0;
int count = 0;
for(int i=0; i<str.length(); i++){
//odd palindrome;
count += find(str, i, i);
// even palindrome
count+= find(str, i, i+1);
}
return count;
}
public int find(String str, int start, int end){
int count = 0;
while(start>=0 && end<str.length()){
if(str.charAt(start) == str.charAt(end)){
count++;
start--;
end++;
} else {
break;
}
}
return count;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CountPalindrome p = new CountPalindrome();
System.out.println(p.countPalindrome("aba"));
System.out.println(p.countPalindrome("abba"));
}
}