Java Solution


  • 0
    H
    public int longestPalindrome(String s) {
    	if (s == null || s.length() == 0) {
    		return 0;
    	}
    	int[] map = new int[256];
    	for (char c : s.toCharArray()) {
    		++map[c];
    	}
    	int mid = 0;
    	int res = 0;
    	for (int i = 0; i < 256; ++i) {
    	     if ((map[i] & 1) == 1) {
    		  mid = 1;
    	          map[i]--;
                      i--;
    	     } else {
    		  res += map[i];
    	     }
    	}
    	return res + mid;
    }
    

Log in to reply
 

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