simple Java solution beats 99%


  • 0
        public int longestPalindrome(String s) {
            if (s.length() == 1) return 1;
            int rtn = 0;
            int[] arr = new int[128];
            for (char c : s.toCharArray()) {
                ++arr[c];
            }
            for (int i : arr) {
                rtn += i / 2;
            }
            rtn *= 2;
            return s.length() == rtn ? rtn : rtn + 1;
        }
    

Log in to reply
 

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