Palindrome Permutation

来源:互联网 发布:电脑版进库出库软件 编辑:程序博客网 时间:2024/06/05 13:22

Given a string, determine if a permutation of the string could form a palindrome.

For example,

"code" -> False, "aab" -> True, "carerac" -> True.

思路:count 奇偶性,把握Palindrome的特性,全部是偶数对,最多只有一个奇数.

public class Solution {     public boolean canPermutePalindrome(String s) {         if(s == null ) return false;         if(s.length() ==0) return true;         HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();         for(int i=0; i<s.length(); i++){             char c = s.charAt(i);             Integer val = hashmap.get(c);             if(val == null){                 hashmap.put(c,1);             } else {                 hashmap.put(c,val+1);             }         }         int count = 0;         for(Integer val : hashmap.values()){             if(val%2 == 1){                 count++;             }         }         return count<=1;     } }


0 0