本文主要是介绍Count Palindrome in String,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 palindromecount+= 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 stubCountPalindrome p = new CountPalindrome();System.out.println(p.countPalindrome("aba"));System.out.println(p.countPalindrome("abba"));}
}
这篇关于Count Palindrome in String的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!