Skip to content

Palindrome Rearranging

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

Example

For inputString = "aabb", the output should be

palindrome_rearranging(inputString) = true

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

Solution

py
def palindrome_rearranging(inputString):
    new_string = []

    for c in inputString:
        if c in new_string:
            new_string.pop(new_string.index(c))
        else:
            new_string.append(c)

    return len(new_string) <= 1


print(palindrome_rearranging("aabbcc"))
js
function palindromeRearranging(inputString) {
  let count = 0;
  let obj = {};
  for (let i = 0; i < inputString.length; i++) {
    if (obj[inputString[i]]) {
      obj[inputString[i]]++;
    } else {
      obj[inputString[i]] = 1;
    }
  }
  for (let key in obj) {
    if (obj[key] % 2 !== 0) {
      count++;
    }
  }
  if (count > 1) {
    return false;
  }
  return true;
}

console.log(palindromeRearranging('aabbcc'));

my thoughts are neither my employer's nor my wife's