You are given a string 'S', check if there exists any permutation of the given string that is a palindrome.
Note :
1. A palindrome is a word or phrase that reads the same from forward and backward e.g. “aba”, it reads the same from forward and backward.
2. A permutation is a rearrangement of letters.
3. The palindrome does not need to be limited to just dictionary words.
Example :
Given string S : aab
The output should be "True" as "aba" (permutation of string S) is a palindrome.
The first line of the input contains an integer 'T' denoting the number of test cases.
The first and the only line of each test case contains one string 'S'.
Output Format :
For each test case print in a new line, “True” if any permutation of the string is a palindrome or “False” if none of the permutations of the given string are palindrome.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= Length of the given string <= 10^5
It is guaranteed that all the characters in the strings are lower case english alphabets.
Time Limit : 1sec
1
carrace
True
The output is True because “racecar” is a permutation of “carrace” and it is a palindrome.
1
ferel
False
Since no permutation of "ferel" is a palindrome hence the output is False.
We can use some special property of the palindromic string.
O(N), where N is the length of the given string.
Firstly we calculate the frequency of each character and for that, we iterate on each character of the string once and for the checking that how many characters are having odd frequency we iterate on each character in the frequency table once and since its size can never be greater than 26, hence the overall time complexity will be O(N).
O(1).
We store the frequency of each character in a frequency table, and only 26 different characters are possible so our complexity turns out to be O(26), which is almost constant and so the space complexity can be considered as O(1).