Problem Solving with Algorithms

반응형

leetcode.com/problems/palindrome-permutation/.   

 

 

class Solution:
    def canPermutePalindrome(self, s: str) -> bool:
        
        
        val = 0
        
        for letter in s:
            val = val ^ 1 << ord(letter)
        
        return val & (val - 1) == 0

 

class Solution:
    def canPermutePalindrome(self, s: str) -> bool:
        map = {}
        for c in s:
            map[c] = map.get(c, 0) + 1
        odd = 0
        for (char, count) in map.items():
            if count % 2 == 1:
                odd += 1
        return odd <= 1

 

 

 

 

 

class Solution:
    def canPermutePalindrome(self, s: str) -> bool:
        count = collections.Counter(s)
        oddcount = 0
        
        for ele,value in count.items():
            if value % 2 != 0:
                oddcount += 1
            if oddcount > 1:
                return False
        return True

 

 

from collections import Counter
from itertools import filterfalse
class Solution:
    def canPermutePalindrome(self, s: str) -> bool:
        return len(list(filterfalse(lambda x:x%2==0,Counter(s).values())))<=1

 

 

class Solution(object):
    def canPermutePalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        dict = {}
        for c in s:
            if c in dict:
                dict[c] += 1
            else:
                dict[c] = 1
        
        count_odd = 0
        count_even = 0
        
        for key in dict:
            if dict[key] % 2 == 0:
                count_even += 1
            else:
                count_odd += 1
                
        if count_odd > 1:
            return False
        return True

 

 


leetcode.com/problems/palindrome-permutation-ii/

반응형
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band