TopCoder problem "ChangingSeats" used in TCHS07 Beta 2 (Division I Level Two)



Problem Statement

    

There's a group of people sitting in a row of seats. Each seat can hold only one person. Your goal is to arrange the people so they're all sitting together and there are no empty seats between people. You are given a String[] s representing the row of seats from left to right. The ith element of s is '.' if the ith seat is empty and uppercase 'X' if it's occupied. Each seat is one unit of length wide. There is no space between adjacent seats.

The distance that one person needs to travel to relocate from the ith seat to the jth seat is the absolute value of (i - j) units. Return the minimum possible total distance in units that the people must travel in order to achieve your goal.

 

Definition

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

Constraints

-s will contain between 1 and 50 characters, inclusive.
-s will contain the characters '.' and uppercase 'X' only.
 

Examples

0)
    
"X.X"
Returns: 1
1)
    
"X.X.XXX"
Returns: 3
Let's number the people from 1 to 5 (left to right). To make them all sit together, we can move person 2 one seat to the right and person 1 two seats to the right. The resulting configuration is "..XXXXX". Another way to achieve this is to move person 1 three seats to the right. Either way, the total distance is 3 units.
2)
    
"....X.X.X.X.XXXXX"
Returns: 10
3)
    
".XXXXX..........X.X.XX......X.XX...."
Returns: 81
4)
    
"...................."
Returns: 0

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10715&pm=7536

Writer:

gevak

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Simple Search, Iteration