Simple Java Solution


  • 1
    C
    public class Solution {
        public int longestPalindrome(String s) {
    
    		if (s == null || s.length() == 0) {
    			return 0;
    		}
    
    		int[] hash = new int[200];
    
    		for (Character ch : s.toCharArray()) {
    			++hash[(int) ch];
    		}
    
    		int count = 0;
    		boolean single = false;
    
    		for (int i : hash) {
    			count += (i >> 1);
    			if ((i & 1) == 1) {
    				single = true;
    			}
    		}
    
    		return (count << 1) + (single ? 1 : 0);
    	}
    }
    

Log in to reply
 

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