Java One Pass Solution


  • 0
    Z
    public class Solution {
        public int longestPalindrome(String s) {
            if(s == null || s.length() == 0){
    	    return 0;
    	}
    	int[] count = new int[256];
    	int sum = 0, odd = 0;
    	for(int i = 0; i < s.length(); i++){
    	    sum++;
    	    if(count[s.charAt(i)] % 2 == 1){
    		odd--;
    	    }
    	    else{
    		odd++;
    	    }
    	    count[s.charAt(i)]++;
    	}
    	return sum - Math.max(0, odd - 1);
        }
    }
    

Log in to reply
 

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