Java Solution : Counter


  • 0
    class Solution {
        public int longestPalindrome(String s) {
    
    		HashMap<Character,Integer> counter = new HashMap<Character,Integer>();
    		for(Character c: s.toCharArray())
    			counter.put(c,counter.getOrDefault(c,0)+1);
    		int nLeave=0;
    		int res =0;
    		for(Map.Entry<Character,Integer> e: counter.entrySet()   ){
    			res+= e.getValue()/2;
    			nLeave+=e.getValue()%2;
    		}
    		return nLeave!=0?2*res+1:2*res;
        }
    }
    

Log in to reply
 

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