1. Diffuse from a starting point to both ends
  2. Palindrome substring can be either even or odd length
  3. Max length should be k-j+1, but when j,k exit while, j--, k++ would extend one step on both ends: k-j-1 would be the right length because of +2 difference.
  4. substring(start, end) would be a start of j+1 to justify the -1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
private int begin, max;

public String longestPalindrome(String s) {
int len = s.length();
if (len < 2) return s;
for (int i = 0; i < s.length() - 1; ++i) {
diffuse(s, i, i); // odd length
diffuse(s, i, i+1); // even
}
return s.substring(begin, begin + max);
}

public void diffuse(String s, int j, int k) {
while (j >= 0 && k < s.length() && s.charAt(j) == s.charAt(k)) {
j--; k++;
}
if (max < k - j - 1) {
max = k - j - 1;
begin = j + 1;
}
}
}