-
-
Save nhtzr/92da5727bdc8bef3f9a698b6ac3f7d5a 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
public class LongestPalindrome { | |
public String longestPalindrome(String s) { | |
if(s.length() <= 1) return s; | |
int top = s.length() - 1; | |
char[] input = s.toCharArray(); | |
String palindrome = String.valueOf(s.charAt(0)); | |
int low = 0; | |
int high = top; | |
for(low = 0; low <= top; low++) { | |
for(high = top; high > low; high--) { | |
if (input[low] != input[high]) continue; | |
for (int k = j, puntTemp = i; k >= i; k--, puntTemp++) { | |
final bool isLastIt = k == low | |
if(isLastIt) { | |
final String candidate = s.substring(i,j); | |
if(candidate.length() >= palindrome.length()) { | |
palindrome = s.substring(i,j+1); | |
if(palindrome.length() >= length-i) return palindrome; | |
} | |
} | |
if (input[puntTemp] != input[k]) break; | |
} | |
} | |
} | |
return palindrome; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment