---
title: "Write a program to check if a given string is a valid palindrome permutation."  
description: "Write a program to check if a given string is a valid palindrome permutation."  
author: "Revati S Misra"  
published: 2023-04-19  
updated: 2023-04-24  
canonical: https://www.mindstick.com/forum/157934/write-a-program-to-check-if-a-given-string-is-a-valid-palindrome-permutation  
category: "java"  
tags: ["java", "programs"]  
reading_time: 2 minutes  

---

# Write a program to check if a given string is a valid palindrome permutation.

Write a [program to check](https://www.mindstick.com/forum/157542/write-a-java-program-to-check-if-a-list-of-integers-contains-only-odd-numbers) if a given [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) is a [valid palindrome](https://www.mindstick.com/forum/158833/write-a-rust-program-to-check-if-a-given-string-is-a-valid-palindrome) permutation.

## Replies

### Reply by Aryan Kumar

```java
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](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-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".


---

Original Source: https://www.mindstick.com/forum/157934/write-a-program-to-check-if-a-given-string-is-a-valid-palindrome-permutation

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
