Created
November 5, 2018 16:51
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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