Java Solution using map


  • 0
    D
    public class Solution {
        public boolean canPermutePalindrome(String s) {
            int count = 0;
    		Map<Character,Integer> map = new HashMap <Character,Integer>() ;
    		for (int i =0; i < s.length(); i++){
    			char c = s.charAt(i);
    			Integer val = map.get(new Character(c));
    			if(val != null){
    				map.put(c, new Integer(val + 1));
    			}else{
    				map.put(c,1);
    			}
    		}
    		for (Map.Entry<Character,Integer> entry : map.entrySet()){
    			if (entry.getValue() %2 !=0){
    				count++;
    			}
    		}
    		return (count>1)? false:true;
        }
    }

Log in to reply
 

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