Yet another palindrome rearrange from CodeSignal. I’m passing 9/10 of the hidden tests. But I’m not passing one and I can’t for the life of me figure out what edge case they have in there that my solution won’t pass. The details are take a string and figure out if it can be rearranged into a palindrome.
I’ve scoured the internet for the test but can’t find it. So sorry in advance I can’t provide the particular test. If I could, I’d take care of it on my own and wouldn’t need the help of those smarter than me.
I’ve set it up so that it should count up the particular instances of each letter. If there’s more than one letter in a string that has an odd number of occurrences, it can not form a palindrome.
I’m aware I’m counting each letter up multiple times in some instances. It may not be the greatest O; I’m not concerned with that atm. (Parameters give 4 seconds for execution time for each test)
I’ve also taken into consideration a case where all letters of a string will be the same but it will be an odd number of letters in that string (such as “aaa”).
The parameters are the string will always be all lower case letters and will be a string with length 1 <= x <= 50.
I’m using JavaScript but happy for any help in any language. Not so much concerned with passing this test as I am figuring out what on earth I’m overlooking.
var arr = inputString.split("");
var test = [];
var count = 0;
for (let i = 0; i < arr.length; i++) {
test = arr.filter((letter) => letter === arr[i]);
if (test.length % 2 !== 0) {
if (inputString.length === test.length) return true
count++
if (count > 1) return false
}
}
return true
}
mnCoder is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.