# Check if a permutation of a string can become a palindrome

All you need to do is check that there's at most one character with an odd number of occurrences. Here's a Java example:

private static boolean canMakePalindrom(String s) {
Map<Character, Integer> countChars = new HashMap<>();

// Count the occurrences of each character
for (char c : s.toCharArray()) {
Integer count = countChars.get(c);
if (count == null) {
count = Integer.valueOf(1);
} else {
count = count + 1;
}
countChars.put(c, count);
}

boolean hasOdd = false;
for (int count : countChars.values()) {
if (count % 2 == 1) {
if (hasOdd) {
// Found two chars with odd counts - return false;
return false;
} else {
// Found the first char with odd count
hasOdd = true;
}
}
}

// Haven't found more than one char with an odd count
return true;
}


EDIT4 (yes - these are ordered to make sense, but numbered by chronological order):
The above implementation has a built in inefficiency. I don't think the first iteration over the string can be avoided, but there's no real reason to keep a count of all the occurrences - it's enough to just keep track of those with the an odd count. For this usecase, it's enough to keep track of each character we encounter (e.g., with a Set), and remove it when we encounter it again. In the worst case, where all the characters in the string are different, the performance is comparable, but in the common case, where there are several occurrences of each character, this implementation improves both time and memory complexity of the second loop (which is now reduced to a single condition) dramatically:

private static boolean canMakePalindrom(String s) {
Set<Character> oddChars = new HashSet<>();

// Go over the characters
for (char c : s.toCharArray()) {
// Record the encountered character:
// If the char was already encountered, remove it -
// this is an even time we encounter it
oddChars.remove(c);
}
}

// Check the number of characters with odd counts:
return oddChars.size() <= 1;
}


EDIT3 (yes - these are ordered to make sense, but numbered by chronological order):
Java 8 provides a fluent streaming API which could be used to create an implementation similar to the Python one-liners below:

private static boolean canMakePalindrom(String s) {
return s.chars()
.boxed()
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()))
.values()
.stream()
.filter(p -> p % 2 == 1)
.count() <= 1;
}


EDIT:
Python built-in functions and comprehension capabilities make this too attractive not to publish this one liner solution. It's probably less efficient than the aforementioned Java one, but is quite elegant:

from collections import Counter

def canMakePalindrom(s):
return len([v for v in Counter(s).values() if v % 2 == 1]) <= 1


EDIT2:
Or, an even cleaner approach as proposed by @DSM in the comments:

from collections import Counter

def canMakePalindrom(s):
return sum(v % 2 == 1 for v in Counter(s).values()) <= 1


Instead of counting how many times each letter occurs, another approach keeps track of whether a letter has occurred an odd or even number of times. If a letter has occurred an even number of times, you don’t need to worry about it, and only need to keep track of the odd occurrences in a set. In Java:

public static boolean canMakePalindrome(String s) {
Set<Character> oddLetters = new HashSet<>();
for ( char c : s.toCharArray() ) {
if ( ! oddLetters.remove(c) ) {
}
}
return oddLetters.size() <= 1;
}


Really all you're looking for is if all (or all but one) of the letters are paired off. As long as they are, then they will be able to be turned into a palindrome.

So it would be something like...

bool canBeTurnedIntoAPalindrome(string drome)
{
// If we've found a letter that has no match, the center letter.
bool centerUsed = false;
char center;

char c;
int count = 0;

// TODO: Remove whitespace from the string.

// Check each letter to see if there's an even number of it.
for(int i = 0; i<drome.length(); i++)
{
c = drome[i];
count = 0;

for(int j = 0; j < drome.length(); j++)
if (drome[j] == c)
count++;

// If there was an odd number of those entries
// and the center is already used, then a palindrome
// is impossible, so return false.
if (count % 2 == 1)
{
if (centerUsed == true && center != c)
return false;
else
{
centerused = true;
center = c;   // This is so when we encounter it again it
// doesn't count it as another separate center.
}
}
}
// If we made it all the way through that loop without returning false, then
return true;
}


This isn't the most efficient (it's counting letters as many times as it comes across them, even if they've been counted already) but it does work.