Skip to content

Instantly share code, notes, and snippets.

@JohnnyFang
Created November 5, 2018 16:51
Show Gist options
  • Save JohnnyFang/77d02c3f44778a6f8e314a0dbb6bd5f9 to your computer and use it in GitHub Desktop.
Save JohnnyFang/77d02c3f44778a6f8e314a0dbb6bd5f9 to your computer and use it in GitHub Desktop.
Given a string, find out if its characters can be rearranged to form a palindrome.
# not so Pythonic way
def palindromeRearranging(inputString):
letter_counts_dict = dict()
for letter in inputString:
if letter in letter_counts_dict:
letter_counts_dict[letter] += 1
else:
letter_counts_dict[letter] = 1
threshold = 0
for k,v in letter_counts_dict.items():
if v % 2 != 0:
threshold += 1
if threshold > 1:
return False
return True
# Pythonic Way
def palindromeRearranging(inputString):
return sum([inputString.count(i)%2 for i in set(inputString)]) <= 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment