2

I am given a string and I have to determine whether it can be rearranged into a palindrome.

For example: "aabb" is true. We can rearrange "aabb" to make "abba", which is a palindrome.

I have come up with the code below but it fails in some cases. Where is the problem and how to fix this?

def palindromeRearranging(inputString):
    a = sorted(inputString)[::2]
    b = sorted(inputString)[1::2]
    return b == a[:len(b)]
Methuse
  • 23
  • 5

5 Answers5

4
def palindromeRearranging(inputString):
    return sum(map(lambda x: inputString.count(x) % 2, set(inputString))) <= 1

this code counts occurrence for every character in string. in palindromes there is one character with odd occurrence if length of string is odd, if length of string is even then no character has odd occurance.

see here

Community
  • 1
  • 1
Mahshid
  • 71
  • 3
  • Nice! I have read a number of solutions and yours is an elegant one. I would also like to know where the problem in my code though. In what cases it would result differently from yours? – Methuse Apr 02 '20 at 10:51
3
def palindromeRearranging(inputString):
   elements = {c:inputString.count(c) for c in set(inputString)}
   even = [e % 2 == 0 for e in elements.values()]
   return all(even) or (len(inputString) % 2 == 1 and even.count(False) == 1)

It counts each character number of appearances, and checks whether all elements appear an even number of times or if the length of the input string is odd, checks whether only one character appears an odd number of times.

DanBrezeanu
  • 523
  • 3
  • 13
1

Python3

def palindromeArrange (string):
    string = list(string)

for i in range (len(string)):
    
    """if the string has even element count"""
    if len(string) % 2 == 0 and len(string)/2 == len (set (string)):
        return True
    """if the string has odd element count"""
    if len(string) - ((len(string)-1)/2) == len (set (string)):
        return True
return False
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 15 '21 at 23:08
0

One liner using list comprehension in Python3

return  len([x for x in set(inputString) if inputString.count(x) % 2 != 0]) <= 1

Basically counts those characters that have counts that aren't divisible by 2.

For even strings it would be zero, and for odd strings, it would be one.

Ola
  • 1
  • 2
0

The solution I can think of right away has time complexity is O(n). The assumption is, palindrome can not be made if there is more than one character with the odd count.

def solution(inputString):

   string = list(inputString)
   n = len(string)
   s_set= set(string)
   
   from collections import Counter

   dic = Counter(string)

   k =0 #counter for odd characters

   for char in s_set:
      if dic.get(char)%2!=0:
         k+=1

   if k>1:
     return False
   else:
     return True