public class LongestPalindromeFinder {

public static void main(String[] args) {
String input = "HYTBCABADEFGHABCDEDCBAGHTFYW12345678987654321ZWETYGDE";
System.out.println(getLongestPalindrome("1234"));
System.out.println(getLongestPalindrome("1223213"));
System.out.println(getLongestPalindrome("9912321456"));
System.out.println(getLongestPalindrome("9912333321456"));
System.out.println(getLongestPalindrome("12145445499"));
System.out.println(getLongestPalindrome(input));
}

private static String getLongestPalindrome(String string) {
String odd = getLongestPalindromeOdd(string);
String even = getLongestPalindromeEven(string);
return (odd.length() > even.length() ? odd : even);
}

public static String getLongestPalindromeOdd(final String input) {
int rightIndex = 0, leftIndex = 0;
String currentPalindrome = "", longestPalindrome = "";
for (int centerIndex = 1; centerIndex = 0 && rightIndex longestPalindrome
.length() ? currentPalindrome : longestPalindrome;
leftIndex--;
rightIndex++;
}
}
return longestPalindrome;
}

public static String getLongestPalindromeEven(final String input) {
int rightIndex = 0, leftIndex = 0;
String currentPalindrome = "", longestPalindrome = "";
for (int centerIndex = 1; centerIndex = 0 && rightIndex longestPalindrome
.length() ? currentPalindrome : longestPalindrome;
leftIndex--;
rightIndex++;
}
}
return longestPalindrome;
}

}

Advertisements