mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-25 18:01:09 -04:00
add some int-cake problems
This commit is contained in:
parent
e04656170b
commit
fa83f7e42a
3 changed files with 86 additions and 0 deletions
23
interview_cake/palindrome.py
Normal file
23
interview_cake/palindrome.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
"""
|
||||
Write an efficient function that checks whether
|
||||
any permutation of an input string is a palindrome.
|
||||
"""
|
||||
|
||||
def has_palindrome_permutation(the_string):
|
||||
unpaired_characters = set()
|
||||
|
||||
for char in the_string:
|
||||
if char in unpaired_characters:
|
||||
unpaired_characters.remove(char)
|
||||
else:
|
||||
unpaired_characters.add(char)
|
||||
|
||||
return len(unpaired_characters) <= 1
|
||||
|
||||
str1 = "civic"
|
||||
print has_palindrome_permutation(str1)
|
||||
print "Should be True"
|
||||
|
||||
str2 = "ivilc"
|
||||
print has_palindrome_permutation(str2)
|
||||
print "Should be False"
|
Loading…
Add table
Add a link
Reference in a new issue