Palindrome Permutation

http://www.lintcode.com/zh-cn/problem/palindrome-permutation/

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Solution {
    /**
     * @param s: the given string
     * @return: if a permutation of the string could form a palindrome
     */
    public boolean canPermutePalindrome(String s) {
        // write your code here
        Map map = new HashMap<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            Integer integer = map.get(c);
            if (integer == null) {
                integer = 1;
            } else {
                integer++;
            }
            map.put(c, integer);
        }
        int count = 0;
        Iterator> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry next = iterator.next();
            if (next.getValue() % 2 != 0) {
                count++;
                if (count >= 2) {
                    return false;
                }
            }
        }
        return true;
    }
}

你可能感兴趣的:(Palindrome Permutation)