CODESPTF - Palindromes
Given a string, you keep swapping any two characters in the string randomly till the string becomes a palindrome. What is the expected number of swaps you will make?
The first line contains the number of test cases T. Each of the next T lines contains a string each.
Output T lines containing the answer for the corresponding test case. Print the answer rounded to exactly 4 decimal places.
T <= 10000
The length of the string will be at most 8 characters.
The string will consist of only lower-case letters 'a'-'z'.
There will always be at least one palindrome which can be formed with the letters of the given string.
For the first two cases, the string is already a palindrome so no swaps are needed.
For the third case, there are 3 possible swaps. The string will become "bab","bba" or remain "abb" with 1/3rd probability each. It's easy to see that the expected number of swaps needed is 3.0000
For the last case, the answer is 59.337962..., which should be printed as 55.3380