public class PalindromePermutation {
public static void main(String[] args) {
String str = "Tact Coa"; // input string
// convert the input string to lowercase and remove all spaces
str = str.toLowerCase().replaceAll("\\s+", "");
// create an array to count the frequency of each character
int[] charCount = new int[128]; // assume ASCII character set
// count the frequency of each character in the input string
for (int i = 0; i < str.length(); i++) {
charCount[str.charAt(i)]++;
}
// count the number of characters with odd frequency
int oddCount = 0;
for (int i = 0; i < charCount.length; i++) {
if (charCount[i] % 2 != 0) {
oddCount++;
}
}
// if the input string is a palindrome permutation, it should have at most one character with odd frequency
if (oddCount > 1) {
System.out.println("Not a valid palindrome permutation");
} else {
System.out.println("Valid palindrome permutation");
}
}
}
This program first converts the input string to lowercase and removes all spaces. It then creates an array to count the frequency of each character in the input string. If the input string is a valid palindrome permutation, it should have at most one character with odd frequency. The program counts the number of characters with odd frequency and outputs whether or not the input string is a valid palindrome permutation. In the example input string above, "Tact Coa" is a valid palindrome permutation because it can be rearranged to form "taco cat".
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
This program first converts the input string to lowercase and removes all spaces. It then creates an array to count the frequency of each character in the input string. If the input string is a valid palindrome permutation, it should have at most one character with odd frequency. The program counts the number of characters with odd frequency and outputs whether or not the input string is a valid palindrome permutation. In the example input string above, "Tact Coa" is a valid palindrome permutation because it can be rearranged to form "taco cat".