Statistics

Problem Statement for "ChangingSeats"

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

  1. "X.X"

    Returns: 1

  2. "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.

  3. "....X.X.X.X.XXXXX"

    Returns: 10

  4. ".XXXXX..........X.X.XX......X.XX...."

    Returns: 81

  5. "...................."

    Returns: 0

  6. "....X...X..X..X....X...X........X"

    Returns: 39

  7. "X.......X.XXX........XX..X.......X.XX.X....X.."

    Returns: 106

  8. ".......X..X..X..X.X...X..XX..XXX.."

    Returns: 47

  9. "...X....X.X.XXX.XXX......X"

    Returns: 19

  10. "XX"

    Returns: 0

  11. "..X..X...XXX..........X.X....X..X"

    Returns: 61

  12. "...X....X...XX.XXX.X..X"

    Returns: 18

  13. "X."

    Returns: 0

  14. ".X..........X.XX.X.X...."

    Returns: 15

  15. ".XX..X.XX.XX....X.....X...X."

    Returns: 37

  16. "XXXXX..XX..X.XX.X..XXX.X.X......XXX...X"

    Returns: 98

  17. "X.....XXXX.XXXXXX...X.X.X..X.....XX..X.XXXX.XXX"

    Returns: 165

  18. "...XXX.XXX.X.X.XX....X.XX.X.X.X..X."

    Returns: 69

  19. "X.XX..XXXXXX...XXX.XX.....XX....XXXXXXXX"

    Returns: 128

  20. "..XXX..X..XX.XXX..X..XX...XXX.XXX..X....X..XXXXX"

    Returns: 152

  21. "X....X....X..X.XX.X.X.XXXX.XXX.XXX.X..X.X"

    Returns: 66

  22. "..XXXXXX.XXX...XXX..XX.XXX.X..."

    Returns: 50

  23. ".X....XX.X..X..X.X.XXXX...XXXX.X"

    Returns: 54

  24. "XX.X..X..XX.XX.XXXXX.....X.....X.X.X...X.XX...XX"

    Returns: 153

  25. "..X.XXXX.XXX...X.XXXXXXXXXX.X.X."

    Returns: 42

  26. "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

    Returns: 0

  27. ".................................................."

    Returns: 0

  28. "...X.............................................."

    Returns: 0

  29. "X"

    Returns: 0

  30. "."

    Returns: 0

  31. "XXXXXXXXXXXXX.........................XXXXXXXXXXXX"

    Returns: 300

  32. "XXXXXXXXXXXXX..............X..........XXXXXXXXXXXX"

    Returns: 302

  33. "XXXXXXXXXXXXXXXX.................XXXXXXXXXXXXXXXXX"

    Returns: 272

  34. "XXXXXXXXXXXXXXXX..................XXXXXXXXXXXXXXXX"

    Returns: 288

  35. "X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X."

    Returns: 156

  36. ".X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X"

    Returns: 156

  37. "XXXX..........................................XXXX"

    Returns: 168


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: