Java Program to Check if Any Anagram of a String is Palindrome or Not

This is the Java Program to Check if Any Anagram of the String is a Palindrome

Problem Description

Given a string, check whether any of its anagrams is palindrome or not. An anagram is just a new word formed by permuting the letters of the string.
Example:

String x = “ACBDABD”

Output = Yes

Problem Solution

Count the frequency of each character in the string. If there is only one character, whose frequency is odd, and rest of the frequencies are even, then print Yes, otherwise print No.

Program/Source Code

Here is the source code of the Java Program to Check if Any Anagram of the String is a Palindrome. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

advertisement
advertisement
  1.  
  2. // Java Program to Check if Any Anagram of the String is a Palindrome
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7.  
  8. public class PalindromeAnagram {
  9.     // Function check if any palindrome anagram exists
  10.     static boolean anagram(String str){
  11.         int[] arr = new int[256];
  12.         int i;
  13.         for(i=0; i<str.length(); i++){
  14.             arr[str.charAt(i)]++;
  15.         }
  16.         int odd,even;
  17.         odd = even =0;
  18.         for(i=0; i<256; i++){
  19.             if(arr[i] % 2 == 0)
  20.                 even++;
  21.             else
  22.                 odd++;
  23.         }
  24.         boolean result;
  25.         if(odd > 1)
  26.             return false;
  27.         return true;
  28.     }
  29.     // Function to read input and display the output
  30.     public static void main(String[] args) {
  31.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  32.         String str;
  33.         System.out.println("Enter the string");
  34.         try {
  35.             str = br.readLine();
  36.         } catch (IOException e) {
  37.             System.out.println("An I/O error occurred");
  38.             return;
  39.         }
  40.         boolean result = anagram(str);
  41.         if(result)
  42.             System.out.println("Palindrome anagrams exist");
  43.         else
  44.             System.out.println("No palindrome anagrams exists");
  45.     }
  46. }
Program Explanation

1. In function anagram(), an array is created to length 256 to count the frequency of various characters as per ASCII code.
2. The loop for(i=0; i<str.length(); i++) traverses through the whole string and counts the frequency of each character.
3. The loop for(i=0; i<256; i++) traverses through the array and count the number of characters having even frequencies and odd frequencies.
4. The condition if(odd > 1) checks if the number of characters having odd frequency is more than 1, false is returned, otherwise true is returned.

Time Complexity: O(n) where n is the length of the string.

Runtime Test Cases
 
Case 1 (Positive Test Case):
 
Enter the string
ACBDABD
Palindrome anagrams exist
 
Case 2 (Negative Test Case):
 
Enter the string
bcddacbaaf
No palindrome anagrams exists

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.