Java 8ms solution, beats 99%.


  • 0
    H
    	public int longestPalindrome(String s) {
    		int[] count = new int['z' - 'A' + 1];
    		int sum = 0;
    		int oddCount = 0;
    		for (char c : s.toCharArray()) {
    			count[c - 'A']++;
    		}
    		for (int n : count) {
    			sum += n / 2;
    			if (n % 2 != 0) {
    				oddCount++;
    			}
    		}
    		sum *= 2;
    		if (oddCount != 0) {
    			sum++;
    		}
    		return sum;
    	}
    

Log in to reply
 

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