Skip to content

Latest commit

 

History

History
77 lines (58 loc) · 1.84 KB

File metadata and controls

77 lines (58 loc) · 1.84 KB

中文文档

Description

Given a string, write a function to check if it is a permutation of a palin­ drome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.

 

Example1:

Input: "tactcoa"

Output: true(permutations: "tacocat"、"atcocta", etc.)

Solutions

Python3

class Solution:
    def canPermutePalindrome(self, s: str) -> bool:
        if s is None or len(s) < 2:
            return True
        cache = {}
        for ch in s:
            cache[ch] = 1 if cache.get(ch) is None else cache[ch] + 1
        cnt = 0
        for k, v in cache.items():
            if (v & 1) == 1:
                cnt += 1
            if cnt > 1:
                return False
        return cnt <= 1

Java

class Solution {
    public boolean canPermutePalindrome(String s) {
        if (s == null || s.length() < 2) {
            return true;
        }
        char[] chars = s.toCharArray();
        Map<Character, Integer> counter = new HashMap<>();
        for (char ch : chars) {
            counter.put(ch, counter.get(ch) == null ? 1 : counter.get(ch) + 1);
        }
        int cnt = 0;
        for (Map.Entry<Character, Integer> entry : counter.entrySet()) {
            if ((entry.getValue() & 1) == 1) {
                ++cnt;
            }
            if (cnt > 1) {
                return false;
            }
        }
        return cnt <= 1;
    }
}

...