물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Permute a Palindrome

BEstyle 2022. 11. 17. 14:32

DESCRIPTION:

Write a function that will check whether ANY permutation of the characters of the input string is a palindrome. Bonus points for a solution that is efficient and/or that uses only built-in language functions. Deem yourself brilliant if you can come up with a version that does not use any function whatsoever.

Example

madam -> True
adamm -> True
junk -> False

Hint

The brute force approach would be to generate all the permutations of the string and check each one of them whether it is a palindrome. However, an optimized approach will not require this at all.


 

def permute_a_palindrome (input): 
    if input=="":
        return True
    max=len(input)%2 #0, 1
    s=''.join(sorted(input))
    count=1
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            count+=1
            if count==len(input):
                return True
        else:
            max-=count%2
            if max<0:
                return False
            count=1
    return True

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Design a simple automation  (0) 2022.11.17
Is the meetup age-diverse?  (0) 2022.11.17
Round by 0.5 steps  (0) 2022.11.16
Collatz  (0) 2022.11.15
Is Integer Array?  (0) 2022.11.15