package com.jt;

public class Palindrome {
    public static String findLongestPalindrome(String text) {
        if (text == null) {
            throw new IllegalArgumentException("text can't be null");
        }

        String longestPalindrome = "";
        int len = text.length();

        for (int i = 0; i < len; i++) {
            for (int j = i; j < len; j++) {
                String subStr = text.substring(i, j);
                if (isPalindrome(subStr) && subStr.length() > longestPalindrome.length()) {
                    longestPalindrome = subStr;
                }
            }
        }

        return longestPalindrome;
    }

    public static boolean isPalindrome(String str) {
        int i = 0;
        int j = str.length() - 1;

        while (i < j) {
            if (str.charAt(i) != str.charAt(j)) {
                return false;
            }

            i++;
            j--;
        }

        return true;
    }
}
