Longest Palindromic Substring
Given a stringS, find the longest palindromic substring inS. You may assume that the maximum length ofSis 1000, and there exists one unique longest palindromic substring.
Example
Given the string ="abcdzdcab", return"cdzdc".
public String longestPalindrome(String s) {
if(s == null || s.length() == 0) {
return "";
}
int n = s.length();
boolean[][] dp = new boolean[n][n];
String res = null;
int max = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n - i; j++) {
int end = j + i;
if(s.charAt(end) == s.charAt(j) && (end - j <= 2 || dp[j + 1][end - 1])) {
dp[j][end] = true;
}
if(end - j + 1 > max && dp[j][end]) {
max = end - j + 1;
res = s.substring(j, end + 1);
}
}
}
return res;
}