Javatpoint Logo
Javatpoint Logo

Check if any anagram of a string is palindrome or not

Problem statement:

Here, we have given an input string, and we have to find if there is any anagram of the given string available which is a palindrome or not? If it is, then it returns true; otherwise, it will return false.

What is an Anagram string?

If we shuffle the characters of a string in random order then the resultant string with the same number of characters is known as the anagram string of the given string.

For example:

Here, the given string str is "javatpoint"

So, there can be various anagrams exist of a given string like "javapointt","tjavapoint","jatavpoitn" etc.

What is a Palindrome?

If a string is read from the backward and forward direction as the same then the string is known as a palindrome string.

The reverse of the palindrome string is the same as the string.

For example:

"abcddbca", "abcdbca" are an example of palindrome strings.

Method 1

The brute force approach to solve the problem is that we will generate all the anagrams of a string. If any of the anagrams are found palindrome, then we will return true; otherwise, we will return false.

Java Code:

Output:

Check if any anagram of a string is palindrome or not

Explanation

In the above code, we have generated all the anagrams of the given string and then checked if any of the anagrams were palindrome, and then we made our answer true.

We used a global Boolean variable to store the result and initially set it to false. We called the function 'anagram', which takes only two arguments, one is the original string, and another is an empty string which will be our anagram after a few steps.

Since the anagram of a string is nothing but just the permutations, we used a recursive approach to get the permutations of a string.

In each recursive call, we will iterate at each character of the string, remove it and add it to our answer string and then call the recursive function for the remaining string.

For the base case, if the given string is empty, it signifies that we have generated the permutation of the given string, and it is stored in the answer string.

Now we will check if the answer string is a palindrome then we will make our result true and return from the function.

To check the palindrome of any string, we will use a two-pointer approach. We use the left pointer from the 0th index and the right pointer from the last index. If any of the characters is mismatching, then we will return false else, move the left pointer to the right and the right pointer to the left side until both cross each other.

Complexity

The time complexity of the above code would be O(!NXN) to generate all the permutations and O(!N*N) to check the palindrome of the string. So, the time complexity would be O(!NXN).

Space complexity: O(1) auxiliary space (if we ignore space used by recursive calls)

Method 2

The optimized approach is quite simple. To be a palindrome string, there should not be more than one character with the odd count.

So we will count the frequency of each character, and if there is more than one character whose frequency is odd, then we will return true otherwise return false.

We can use either a frequency array or hashmap to store the frequency.

Java Code:

Output:

Check if any anagram of a string is palindrome or not

Explanation

  • In the above code, we used a hashmap of character vs integer to store the frequency of each character which is present in the given string.
  • After storing the frequency of all the characters, we will iterate to the hashmap using the keySet() function, and we will get the frequency of each key.
  • If the frequency of the key is odd then we will increase the count.
  • In the end, if the count is greater than one then it is sure that no anagram exists, which is a palindrome so that we will print false otherwise, we will print true.

Time complexity: O(N), where N is the number of characters in the string.

Space complexity: O(K), where K is the number of unique characters in the string.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA