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.
public class Solution {
public String longestPalindrome(String s) {
if(s == null || s.length() == 0) {
return s;
}
int len = s.length(), start = 0, max = 1;
boolean[][] map = new boolean[len][len];
for(int i = 0; i < len; i++) {
map[i][i] = true;
}
for(int i = 0; i < len - 1; i++) {
if(s.charAt(i) == s.charAt(i + 1)) {
map[i][i + 1] = true;
start = i;
max = 2;
}
}
for(int l = 3; l <= len; l++) {
for(int i = 0; i <= len - l; i++) {
int j = i + l - 1;
if(s.charAt(i) == s.charAt(j) && map[i + 1][j - 1]) {
map[i][j] = true;
start = i;
max = l;
}
}
}
return s.substring(start, start + max);
}
}