Palindrome Permutation

Palindrome Permutation

 
Write a function to check if any combination of characters of the given string can be a palindrome.
 
e.g. Tact Coa

Output : True

Explanation: “atco cta” can be made by rearranging the characters of string “Tact Coa”.
 
Here is the code.
 



public class PalindromePermutation {
public static void main(String[] args) {
System.out.println(checkPP("Tact Coa"));
}
private static boolean checkPP(String string) {
int[] bitArray = new int[128];
int count = 0;
for (char c : string.toCharArray()) {
int index = Character.getNumericValue(c);
if (index >= 0) {
bitArray[index]++;
if (bitArray[index] % 2 == 1) {
count++;
} else {
count--;
}
}
}
if (count <= 1) {
return true;
}
return false;
}
}


Output:

No Comments Yet

Leave a Reply

Your email address will not be published.

Lorem ipsum dolor sit amet, consectetur a dipiscing elit. Vivamus leo ante,

FOLLOW US ON