package no005.longest.palindrom.substring;

/**
 * Created by jsong on 28/03/2018.
 *
 * @hackerrank https://www.hackerrank.com/jsong00505
 * @backjoon https://www.acmicpc.net/user/jsong00505
 * @github https://github.com/jsong00505
 * @linkedin https://www.linkedin.com/in/junesongskorea/
 * @leetcode https://leetcode.com/jsong00505/
 * @email jsong00505@gmail.com
 * @challenge Longest Palindrome Substring
 */
public class Solution {

  /**
   * Validate if the substring of s is a palindrome string between supplied indexs.
   *
   * @param s A string
   * @param start A start index
   * @param end A end index
   * @return {@link true} If it is a plindrome string of {@link false} if not. If the length is 1 it
   *     treats as like a palindrome string.
   */
  public static boolean isPalindrome(String s, int start, int end) {
    int length = end - start + 1;
    if (length == 1) {
      return true;
    }
    if (s.charAt(start) == s.charAt(end)) {
      return length == 2 || isPalindrome(s, start + 1, end - 1);
    }
    return false;
  }

  public static String longestPalindrome(String s) {
    int length = s.length();
    for (int i = length - 1; i > 0; i--) {
      for (int j = 0; j <= length - i - 1; j++) {
        if (isPalindrome(s, j, i + j)) {
          return s.substring(j, j + i + 1);
        }
      }
    }
    return s.substring(0, 1);
  }

  public static void main(String[] args) {
    System.out.println("bab".equals(longestPalindrome("babad")));
    System.out.println("aba".equals(longestPalindrome("aba")));
    System.out.println("bb".equals(longestPalindrome("cbbd")));
    System.out.println("a".equals(longestPalindrome("abcde")));
  }
}

@@ -648,16 +648,17 @@
 t is a p
+a
 lindrome
@@ -666,17 +666,17 @@
 string o
-f
+r
  %7B@link 

fix typo