Fine Longest Palindrome Substring of a given String
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
class Solution { | |
public String longestPalindrome(String s) { | |
if(s==null || s.length()==0) { | |
return new String(); | |
} | |
int i = 0; | |
int j = 0; | |
int left = 0; | |
int right = 0; | |
boolean[][] isPalindrome = | |
new boolean[s.length()][s.length()]; | |
for(j=0;j<s.length();j++) { | |
for(i=0;i<j;i++) { | |
boolean isInnerPalindrome = | |
isPalindrome[i+1][j-1] || j-i<=2; | |
if(s.charAt(i)==s.charAt(j) && isInnerPalindrome) { | |
isPalindrome[i][j] = true; | |
if(j-i>right-left) { | |
left = i; | |
right = j; | |
} | |
} | |
} | |
} | |
return s.substring(left,right+1); | |
} | |
} |
Comments
Post a Comment