public static String longestPalindrome1(final String input) {
        /*
           Algorithm:
           1. Start with the second element and compare (i-1) with (i+1)
           if they are equal then we have palindrome so continue comparing (i-1-1) to (i+1+1) till we find palindrome
           else move to the next character and repeat the process.
           http://articles.leetcode.com/longest-palindromic-substring-part-i/
         */

        char[] chars = input.toCharArray();
        String longest = String.valueOf(chars[0]);
        for (int middle = 1; middle < input.length(); middle++) {
            int left = middle - 1;
            int right = middle + 1;
            if (left >= 0 && right < input.length() && chars[left] == chars[right]) {
                String possibleLongest = findLongest(chars, left, right);
                if (longest.length() < possibleLongest.length()) {
                    longest = possibleLongest;
                }
            }
            if (middle >= 0 && right < input.length() && chars[middle] == chars[right]) {
                String possibleLongest = findLongest(chars, middle, right);
                if (longest.length() < possibleLongest.length()) {
                    longest = possibleLongest;
                }
            }
        }

        return longest;
    }
Given a string S, find the longest palindromic substring in S.
You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
java8/src/main/java/com/shekhargulati/leetcode/algorithms/Problem05.java