Created
May 3, 2020 21:59
-
-
Save almaax33/6bf0a82c9c3e50d7a5b1b07dfa7d7856 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) { | |
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