Longest Palindromic Substring
Description
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
public class Solution {
/**
* @param s: input string
* @return: a string as the longest palindromic substring
*/
public String longestPalindrome(String s) {
// write your code here
if(s.length() == 1){
return s ;
}
String result = null ;
int len = s.length() ;
int maxlen = 0 ;
boolean isP = true ;
// int left = 0 , right = 0 ;
for(int left = 0 ; left < len ; left++){
for(int right = left ; right < len ; right++){
if(maxlen >= right - left +1){
continue ;
}
isP = isP(s , left , right) ;
if(isP){
maxlen = right - left +1 ;
result = s.substring(left , right+1) ;
}
}
}
return result ;
}
public boolean isP(String s , int left , int right){
for(int i = left , j = right ; i < j ; i++ , j--){
if(s.charAt(i) != s.charAt(j)){
return false ;
}
}
return true ;
}
}