Skip to content

Latest commit

 

History

History
52 lines (37 loc) · 903 Bytes

0266._Palindrome_Permutation.md

File metadata and controls

52 lines (37 loc) · 903 Bytes

266. Palindrome Permutation

难度: Easy

刷题内容

原题连接

内容描述

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

Example 1:

Input: "code"
Output: false
Example 2:

Input: "aab"
Output: true
Example 3:

Input: "carerac"
Output: true

解题方案

思路 1 - 时间复杂度: O(N)- 空间复杂度: O(N)******

数一下只出现奇数次数的字母有多少个,如果少于或者等于1个,都可以组成Palindrome

beats 100%

class Solution:
    def canPermutePalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        tmp = collections.Counter(s)
        res = 0
        for i in tmp.values():
            if i & 1 == 1:
                res += 1
        return res <= 1