Beat 100% Java Solution, and easy to understand


  • 9
    N
    public String longestPalindrome(String s) {
        if(s==null){
            return "";
        }
       char[] arr = s.toCharArray();
    	  int max = 0;
    	  int maxi = 0;
    	  int maxj = 0;
    	  
    	  for(int i = 0; i< arr.length;){
    		  int i1 = getFarestSameElementIndex(arr,i);
    		  int dist = getDistance(arr,i,i1);
    		  int index1 = i-dist;
    		  int index2 = i1 + dist;
    		  int l = index2 - index1;
    		  if(l>max){
    		          max = l;
    			  maxi = index1;
    			  maxj = index2;
    		  }
    		  i = i1+1;
    	  }
    	  
    	  return s.substring(maxi, maxj+1);
    }
    
    private int getDistance(char[] arr,int index1,int index2){
    	int i1 = index1-1;
    	int i2 = index2+1;
    	int dist = 0;
    	while(i1>=0&&i2<arr.length){
    		if(arr[i1]==arr[i2]){
    			dist++;
    		}else{
    			break;
    		}
    		i1--;i2++;
    	}
    	return dist;
    }
    
    private int getFarestSameElementIndex(char[] arr, int index){
    	for(int i = index+1;i<arr.length;i++){
    		if(arr[i]!=arr[index]){
    			return i-1;
    		}
    	}
    	return arr.length-1;
    }

  • 0
    N

    Sorry to use the static method here.


  • 0
    A

  • 0
    Y

    This solution is based on the assumption that "there exists one unique longest palindromic ".


  • 0
    C

    good way.
    Start checking from the middle one. Thanks


  • 0
    C

    but I think the time base one the test case. if there are less same letters in the word, the time will take off.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.