Problem05 { public static String longestPalindrome1(final String input) { 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; }  static String longestPalindrome(final String input); static String longestPalindrome1(final String input);  }
@Test public void shouldFindLongestPalindromeString() throws Exception { final String input = "racecar"; String s = Problem05.longestPalindrome1(input); assertThat(s, equalTo("racecar")); }
@Test public void shouldFindLongestPalindromeString_2() throws Exception { final String input = "saas"; String s = Problem05.longestPalindrome1(input); assertThat(s, equalTo("saas")); }
@Test public void shouldFindLongestPalindromeString_3() throws Exception { final String input = "forgeeksskeegfor"; String s = Problem05.longestPalindrome1(input); assertThat(s, equalTo("geeksskeeg")); }
@Test public void shouldFindLongestPalindromeString_4() throws Exception { final String input = "abaccddccefe"; String s = Problem05.longestPalindrome1(input); assertThat(s, equalTo("ccddcc")); }