public static String longestPalindrome (String s) {
    char[] S = s.toCharArray();
    if (s == null || s == "" || S.length == 0) return "";
    boolean T[][] = new boolean[S.length][S.length];
    int maxi = 0;
    int maxLen = 1;
    // len = 1
    Arrays.fill(T[0], true);
    //len = 2
    for (int i=0; i<S.length-1; i++) {
      if (S[i] == S[i+1]) {
        T[1][i] = true;
        maxi = i;
        maxLen = 2;
      }
    }
    // len >= 3
    for (int len=3; len<=S.length; len++) {
      for (int i=0; i< S.length - (len-1); i++) {
        if (T[len-1-2][i+1] && S[i] == S[i+len-1]) {
          T[len-1][i] = true;
          maxi = i;
          maxLen = len;
        }
      }
    }
    return s.substring(maxi, maxi + maxLen);
  }