Java solution Beats 94%


  • 0
    W
    public class Solution {
        public int longestPalindrome(String s) {
            if (s == null || s.length() == 0) return 0;
            int[] map = new int[128];
            char[] chars = s.toCharArray();
            int res = 0;
            boolean hasOdd = false;
            for (char c : chars) map[c]++;
            for (int i = 0; i < 128; i++) {
                if (map[i] % 2 == 0) res += map[i];
                else {
                    res += map[i] - 1;
                    hasOdd =  true;
                }
            }
            return hasOdd ? res + 1 : res;
        }
    }

Log in to reply
 

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