Simple Java HashMap solution


  • 0
    Q

    The idea is to remove the min number of extra chars those can't be used to form a palindrome word.

    public class Solution {
        public int longestPalindrome(String s) {
            Map<Character, Integer> map = new HashMap();
            for(char c : s.toCharArray()){
                if(map.containsKey(c))
                    map.remove(c);
                else 
                    map.put(c, 1);
            }
            int count = map.size();
            if(count <= 1) return s.length();
            else return s.length() - count + 1;
        }
    }
    

Log in to reply
 

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