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

«
»

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”

advertisement

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
  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.

advertisement
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
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn