cetmyeval69
Honorary Poster

COMMENT YOUR OWN SOLUTION!


[CODE lang="java" title="My Solution"]public static String longestPalindrome(String input) {
int n = input.length();
boolean[] P = new boolean[n];
int lo = 0, hi = 0, length = 1;
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= i; ++j) {
if (input.charAt(i) == input.charAt(j) && (i - j <= 1 || P[j + 1])) {
P[j] = true;
if (i - j + 1 > length) {
lo = j;
hi = i;
length = i - j + 1;
}
} else {
P[j] = false;
}
}
}
return input.substring(lo, hi + 1);
}[/CODE]
Implementation
Screenshot:
