18. palindromeRearranging


Task

Given a string, find out if its characters can be rearranged to form a palindrome.

Example

For inputString = "aabb", the output should be
palindromeRearranging(inputString) = true.

We can rearrange "aabb" to make "abba", which is a palindrome.

Input/Output

  • [execution time limit]
    4 seconds (py3)

  • [input] string inputString
    A string consisting of lowercase English letters.
    Guaranteed constraints: 1 ≤ inputString.length ≤ 50.

  • [output] boolean
    true if the characters of the inputString can be rearranged to form a palindrome, false otherwise.

My Solution

def palindromeRearranging(inputString):
    count = {}
    oddCount = 0
    for letter in inputString:
        if letter in count:
            count[letter] += 1
        else:
            count[letter] = 1
    for val in count.values():
        if val % 2 == 1:
            oddCount += 1
    return oddCount < 2