TopCoder problem "CharSwaps" used in East China Round 2 (Division I Level Two)



Problem Statement

    

You are given a String s containing only the letters 'a' and 'b'. The letters are arranged in a circle, so the last and first characters are adjacent.

You will perform a series of swaps until all the 'a's form one consecutive sequence and all the 'b's form another consecutive sequence. In each swap, you can select any two characters and swap them. Return the minimal number of swaps necessary to reach your goal.

 

Definition

    
Class:CharSwaps
Method:getMinCount
Parameters:String
Returns:int
Method signature:int getMinCount(String s)
(be sure your method is public)
    
 

Constraints

-s will contain between 1 and 15 characters, inclusive.
-s will contain the letters 'a' and 'b' only.
 

Examples

0)
    
"ba"
Returns: 0
The letters are already arranged properly in this string.
1)
    
"aaaabbbbba"
Returns: 0
The letters are already arranged properly in this string as well.
2)
    
"abab"
Returns: 1
Swap the first two letters.
3)
    
"aabbaaabaaba"
Returns: 2
4)
    
"abababababababa"
Returns: 3
The case with the largest possible answer.
5)
    
"aaaa"
Returns: 0

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=9717

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=12227&pm=9717

Writer:

Mike Mirzayanov

Testers:

PabloGilberto , Olexiy , ivan_metelsky

Problem categories:

Search