patternpythonMinor
Check if a string is a permutation of a palindrome using Python
Viewed 0 times
palindromeusingpythonpermutationcheckstring
Problem
This is a solution to exercise 1.4 from Cracking the Coding Interview written using Python 3.
Given a string, write a function to check if it is a permutation of a palindrome.
Example: 'Tact Coa'
Output: True (permutations: "taco cat", "atco cta", etc.)
I wanted to get feedback on making my code clearer and more pythonic. I believe my code has time complexity \$O(n)\$ and space complexity \$ O(n)\$ is that correct? If so, is there any way to improve on either the space or time complexity?
Test
datum = 'Tac4t co@A @4'
print(is_palindrome_permutation(datum))
Result
True
Test
datum = 'Tac4t co@A @4s'
print(is_palindrome_permutation(datum))
Result
False
Given a string, write a function to check if it is a permutation of a palindrome.
Example: 'Tact Coa'
Output: True (permutations: "taco cat", "atco cta", etc.)
I wanted to get feedback on making my code clearer and more pythonic. I believe my code has time complexity \$O(n)\$ and space complexity \$ O(n)\$ is that correct? If so, is there any way to improve on either the space or time complexity?
from collections import Counter
def is_palindrome_permutation(data: str) -> bool:
"""Given a string, check if it is a permutation of a palindrome."""
data = data.lower().replace(' ', '')
num_odd = sum(1 for char, freq in Counter(data).items() if char != ' ' and freq % 2 == 1)
# Check for two types of palindromes , 1) Odd Length (e.g. abc cba) 2) Even Length (e.g. abc d cba)
if num_odd == 1 and len(data) % 2 == 1 or num_odd == 0 and len(data) % 2 == 0:
return True
else:
return FalseTest
datum = 'Tac4t co@A @4'
print(is_palindrome_permutation(datum))
Result
True
Test
datum = 'Tac4t co@A @4s'
print(is_palindrome_permutation(datum))
Result
False
Solution
Some suggestions:
So the code can be simplified to:
One more thing you could do is put the Counter
- You strip out spaces, then on the next line check if each character is a space. The second part is redundant.
- The only possible values for
freq % 2are0and1, sonum_odd = sum(1 for char, freq in Counter(data).items() if char != ' ' and freq % 2 == 1)can be simplified asnum_odd = sum(freq % 2 for Counter(data).values())
- If
num_oddis0, then there must be an even number of elements. Ifnum_oddis1, then there must be an odd number of elements. So really all you care about is if there is more than 1 odd count.
- You can return the results of expressions directly. So something like
return x
- I would put the replace
first since that reduces the number of characters thatlowerhas to operate over (although this is probably a silly negligible micro optimization).
So the code can be simplified to:
from collections import Counter
def is_palindrome_permutation(data: str) -> bool:
"""Given a string, check if it is a permutation of a palindrome."""
data = data.replace(' ', '').lower()
return sum(freq%2 for freq in Counter(data).values()) < 2One more thing you could do is put the Counter
on the first line. I think this hurts readability a bit, but does mean that your modified data can be garbage collected right away. There is no point doing this with .values(), though, since this is just a view into the Counter object, so the Counter` object has to be kept in memory.from collections import Counter
def is_palindrome_permutation(data: str) -> bool:
"""Given a string, check if it is a permutation of a palindrome."""
data = Counter(data.replace(' ', '').lower())
return sum(freq%2 for freq in data.values()) < 2Code Snippets
from collections import Counter
def is_palindrome_permutation(data: str) -> bool:
"""Given a string, check if it is a permutation of a palindrome."""
data = data.replace(' ', '').lower()
return sum(freq%2 for freq in Counter(data).values()) < 2from collections import Counter
def is_palindrome_permutation(data: str) -> bool:
"""Given a string, check if it is a permutation of a palindrome."""
data = Counter(data.replace(' ', '').lower())
return sum(freq%2 for freq in data.values()) < 2Context
StackExchange Code Review Q#141486, answer score: 6
Revisions (0)
No revisions yet.