For a string, count the number of occurences of each character. If at most one character has odd count then the string can be a palindrome else it's not.
public class Palindrome {
//count the chars in the input string
//if not more than one character has odd count then palindrome is possible
public static boolean isPalindromPossible(String input)
{
int[] charCount = new int[128];
for(int i = 0 ; i < input.length() ; i++)
{
charCount[(int)input.charAt(i)]++;
}
int oddCount = 0 ;
for(int i = 0 ; i < 128 ; i++)
{
if(charCount[i] % 2!=0)
{
oddCount++;
}
}
if(oddCount != 0 && oddCount != 1)
{
return false;
}
return true;
}
public static void main(String[] args) {
String input = "aacaacc";
System.out.println(isPalindromPossible(input));
}
}