Simple Java solution using Hashtable


  • 0
    B
    public static boolean canPermutePalindrome(String s) {
    	Hashtable<Character,Integer> ht=new Hashtable<Character,Integer>();
    	for(int i=0;i<s.length();i++) { 
    		if(ht.containsKey(s.charAt(i))) {
    			int num=ht.get(s.charAt(i));
    			if(num==1) 
    				ht.remove(s.charAt(i));
    			else
    			ht.put(s.charAt(i),num-1);
    		}
    		else {
    			ht.put(s.charAt(i), 1);
    		}		 
    	}
    	return ht.size()==0? true: ht.size()==1? (ht.elements().nextElement().intValue()==1?true:false):false;
            
        }

Log in to reply
 

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