Skip to main content
1 of 2
jonrsharpe
  • 13.9k
  • 2
  • 36
  • 62

You actually have an error in your code; what if the input string has an even length? You set:

isOdd = True

but don't check whether that is the case.


The approach you are taking is to remove characters until at most one of them has an odd count. Once you realise that, the solution becomes obvious:

from collections import Counter

s = input() # don't call your own variable 'str'

c = Counter(s)

odds = [None for n in c.values() if n % 2]

if odds:
    print(len(odds) - 1)
else:
    print(0)

collections.Counter is a dict subclass to make counting the letters simpler. You don't actually need to create the substrings or check whether they are peragrams.


You should read and consider following the style guide, PEP-0008. For example:

if(IsPalindrome(str)):

should be:

if is_palindrome(s):
jonrsharpe
  • 13.9k
  • 2
  • 36
  • 62