Skip to content

Instantly share code, notes, and snippets.

@almaax33
Created May 3, 2020 21:59
Show Gist options
  • Save almaax33/6bf0a82c9c3e50d7a5b1b07dfa7d7856 to your computer and use it in GitHub Desktop.
Save almaax33/6bf0a82c9c3e50d7a5b1b07dfa7d7856 to your computer and use it in GitHub Desktop.
public class LongestPalindrome {
public String longestPalindrome(String s) {
int length = s.length() - 1;
if(length <= 0) return s;
char[] input = s.toCharArray();
String palindrome = String.valueOf(s.charAt(0));
for(int i = 0; i<=length; i++){
for(int j = length; j>i; j--) {
if (input[i] != input[j]) continue;
for (int k = j, puntTemp = i; k >= i; k--, puntTemp++) {
if(k == i){
if(s.substring(i,j).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