JOIN
Get Time

   Problem Statement  

 Problem Statement for PasswordXPalindrome

Problem Statement

    Ms. Ciel loves rabbits. She has a large special cage for her rabbits. The special cage is protected by a secret password consisting of exactly X characters. Each character of her password is one of 'a'-'z'. Due to the security policy of the cage, each of the letters 'a'-'z' only occurs at most twice in the password.



Bored with her current password, Ms. Ciel wants to change her password into a palindrome. (A string is called palindrome if it reads the same forwards and backwards.) To change the password, Ms. Ciel may perform this operation (that consists of two steps) zero or more times:



  1. Choose two different integers A and B such that 0 <= A < B < X.
  2. Swap the characters at (0-based) positions A and B of the current password.


You are given a String password consisting of X characters which represents Ms. Ciel's initial password. Return the minimum number of operations Ms. Ciel needs to change password into a palindrome. If this is not possible, return -1 instead.
 

Definition

    
Class:PasswordXPalindrome
Method:minSwaps
Parameters:String
Returns:int
Method signature:int minSwaps(String password)
(be sure your method is public)
    
 

Constraints

-password will contain between 1 and 50 characters, inclusive.
-Each character of password will be one of 'a'-'z'.
-Each of the letters 'a'-'z' will occur at most twice in password.
 

Examples

0)
    
"levle"
Returns: 1
One possible solution is to swap the last two characters to change the password into "level".
1)
    
"racecar"
Returns: 0
The password is already a palindrome.
2)
    
"abcdadcb"
Returns: 3
One possible solution:
  1. Swap the characters at positions 4 and 7. The password becomes "abcdbdca".
  2. Swap the characters at positions 5 and 6. The password becomes "abcdbcda".
  3. Swap the characters at positions 4 and 6. The password becomes "abcddcba".
3)
    
"msmscielciel"
Returns: 5
4)
    
"hicanyouguesstodaywriter"
Returns: -1
It is not possible to change this password into a palindrome.

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.

This problem was used for:
       2012 TCO Algorithm Round 1C - Division I, Level Three