Last active
April 13, 2016 05:36
-
-
Save nhnpro/fd6f3c4052658ecca34155d7c46382f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static int longestPalindrome(String str) | |
{ | |
int N = str.length(); | |
int maxLength = 1; | |
if(N > 1) | |
{ | |
int[] solution = new int[N]; | |
for (int i = 1; i < N; i++) { | |
char ci = str.charAt(i); | |
for (int j = 0; j < i; j++) { | |
if(str.charAt(j) == ci) | |
{ | |
int maxKIndex = j + 1; | |
for (int k = j + 2; k < i; k++) { | |
if(solution[maxKIndex] < solution[k]) maxKIndex = k; | |
} | |
if(solution[maxKIndex] == 0) | |
solution[j] = maxof3(solution[j], 2 + (i - j > 1 ? 1 : 0), 1); | |
else | |
solution[j] = maxof3(solution[j], 2 + solution[maxKIndex], 1); | |
//println("Char " + j +" c>" + ci + ">" + solution[j] + ">|" + Array2String(solution, "")); | |
} | |
} | |
} | |
for (int i = 0; i < N; i++) { | |
if(maxLength < solution[i]) | |
maxLength = solution[i]; | |
} | |
// println(Array2String(solution, "")); | |
} | |
else | |
maxLength = N; | |
return maxLength; | |
} | |
private static int maxof3(int a, int b, int c) | |
{ | |
int max = a; | |
if(b > max) max = b; | |
if(c > max) max = c; | |
return max; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment