查找最大回文

public class Palindrome {
    public String findPalindrome(String str) {
        String maxString = "";
        for (int i = 0; i < str.length(); i++) {
            for (int j = i; j < str.length() + 1; j++) {
                String tmp = str.substring(i, j);
                if (isPalindrome(tmp) && tmp.length() > maxString.length()) {
                    maxString = tmp;
                }
            }
        }

        if (maxString.length() > 1) {
            return maxString;
        }
        return null;
    }

    public boolean isPalindrome(String str) {
        StringBuffer sb = new StringBuffer(str);
        return str.equals(sb.reverse().toString());
    }

    public static void main(String[] args) {
        Palindrome p = new Palindrome();
        System.out.println(p.findPalindrome("ABCBCA"));
    }

}
posted @ 2018-04-08 15:09  _星辰、  阅读(216)  评论(0编辑  收藏  举报