// 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 {
    public String longestPalindrome(String s) {
    	if(s.length() == 0){
    		return s;
    	}

    	int len = s.length();

    	int max = 0;
    	int oddLen, evenLen, curLen, pos;
    	for(int i = 0; i < len; i++){
    		oddLen = palinLen(s, i, i);

    		if(i + 1 < len){
    			evenLen = palinLen(s, i, i + 1);
    		}

    		curLen = oddLen > evenLen? oddLen : evenLen;

    		if(curLen > max){
    			max = curLen;

    			if(max % 2 == 0){
    				pos = i - (max - 1) / 2;
    			}else{
    				pos = i - max / 2;
    			}
    		}
    	}

    	return s.substring(pos, pos + max);
    }

    public int palinLen(String s, int i, int j){
    	int len = 0;

    	while(i >= 0 && j < s.length() && s.charAt(i) == s.charAt(j)){
    		i--;
    		j++;
    	}

    	i++;
    	j--;
    	len = j - i + 1;
    	return len;
    }
}
@@ -356,16 +356,20 @@
 t oddLen
+ = 0
 , evenLe
@@ -373,16 +373,20 @@
 nLen
+ = 0
 , curLen
 , po
@@ -381,21 +381,29 @@
 , curLen
+ = 0
 , pos
+ = 0
 ;%0A    %09f
@@ -1039,9 +1039,1138 @@
 ;%0A    %7D%0A
-
 %7D
+%0A%0A%0Apublic class Solution %7B%0A    public String longestPalindrome(String s) %7B%0A        int len = s.length();%0A        if (len == 0) %7B%0A            return %22%22;%0A        %7D else if (len == 1) %7B%0A            return s;%0A        %7D%0A        %0A        String res = %22%22;%0A        int maxLen = 0;%0A        %0A        // iterate through s, each char as center, find longest palindrome.%0A        for (int i = 0; i %3C len; i++) %7B%0A            String tempString = palinHelper(s, i, i);%0A            if (tempString.length() %3E maxLen) %7B%0A                maxLen = tempString.length();%0A                res = tempString;%0A            %7D%0A            %0A            tempString = palinHelper(s, i, i + 1);%0A            if (tempString.length() %3E maxLen) %7B%0A                maxLen = tempString.length();%0A                res = tempString;%0A            %7D%0A        %7D%0A        %0A        return res;%0A    %7D%0A    %0A    public String palinHelper(String s, int start, int end) %7B%0A        while (start %3E= 0 && end %3C= s.length() - 1 && s.charAt(start) == s.charAt(end)) %7B%0A            start--;%0A            end++;%0A        %7D%0A        %0A        return s.substring(start + 1, end);%0A    %7D%0A%7D        %0A        

update longest palindromic substring solution