25ms java solution using HashMap


  • 0
    B
        public int longestPalindrome(String s) {
            HashMap<Character, Integer> map = new HashMap<>();
            int count = 0;
            for(int i=0;i<s.length();i++){
                char c = s.charAt(i);
                if(map.containsKey(c) && map.get(c) == 1){
                    count++;
                    map.put(c,0);  
                }
                else map.put(c,1);
            }
            if(count*2 < s.length()) count = count*2 + 1;
            else count = count*2;
            return count;
        }
    }

Log in to reply
 

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