Palindrome Permutation

来源:互联网 发布:在淘宝上怎么买盗版书 编辑:程序博客网 时间:2024/05/16 17:00
public class Solution {    public boolean canPermutePalindrome(String s) {        if (s == null || s.length() < 2) {            return true;        }        Map<Character, Boolean> map = new HashMap<>();        int oddCount = 0;        for (int i = 0; i < s.length(); i++) {            char c = s.charAt(i);            if (!map.containsKey(c)) {                map.put(c, false);                oddCount++;            } else {                if (map.get(c)) {                    map.put(c, false);                    oddCount++;                } else {                    map.put(c, true);                    oddCount--;                }            }        }        boolean evenStr = s.length() % 2 == 0;        if (evenStr) {            if (oddCount == 0) {                return true;            } else {                return false;            }        } else {            if (oddCount == 1) {                return true;            } else {                return false;            }        }    }}

0 0
原创粉丝点击