Statistics

Problem Statement for "RandomRide"

Problem Statement

I go on random bike rides. I go down my driveway, and then at each choice point I flip a coin: Heads means to choose the leftmost road, Tails means to choose the rightmost. If there are 3 choices at the intersection, I flip twice: Heads Heads means to take the leftmost, Heads Tails means to choose the middle road, Tails Heads means choose the rightmost, and Tails Tails is a "do-over" -- repeat the whole process until a choice is made.

The ride continues until the random ride leads me back up my driveway to my house. Here is a map of my neighborhood. All of the intersections where a choice can be made are shown with a capital letter and my home is marked "Home".

                                                   
                                                     
                          +-----M-----N----------+ 
                          |     |     |          | 
                          I-----J-----K----------L 
                          |     |     |          |
                    +-----G-----H     |          |
                    |     |     |     |          |
              +-----B-----C-----D-----E---+      |
              |                           |      |
Home ---------A                           +------F
              |                                  |
              |                                  | 
              +----------------------------------+
Instead of actually flipping a coin while riding my bike, I record the results of a sequence of flips to use during the ride. If during my ride I use them all then I start back at the begining of the sequence. Given flips return the number of flips needed to get me back home. If I never will get back home, return -1.

Definition

Class:
RandomRide
Method:
flipCount
Parameters:
String
Returns:
int
Method signature:
int flipCount(String flips)
(be sure your method is public)

Constraints

  • flips will contain between 1 and 50 characters, inclusive.
  • Each character in flips will be 'H' or 'T'.

Examples

  1. "H"

    Returns: 10

    Every choice will be left. My first flip is at A where my driveway hits the road. Then I go left around the curve and flip at B. My path continues going clockwise around the perimeter of my neighborhood, visiting intersections A, B, G, I, M, N, L, F, and A at which point my flip sends me home. Only at intersection G is there a 3-way choice, thus requiring 2 flips. So I used H H HH H H H H H H using a total of 10 flips at the 9 visited intersections.

  2. "HHTTTTTHTHTHHHTT"

    Returns: 11

    At A I turn left, go around the curve to B and go left there to G. This is an intersection where there are 3 choices. I flip TT (do-over), TT (another do-over), then TH so I turn right and go to C. At C the flip causes me to go right to B where I go "left" (it's really straight, but it is more to the left than the other choice) to A. Now I flip tails so I turn right and am home. The whole trip is A:H, B:H, G:TT TT TH, C:T, B:H, A:T for a total of 11 flips.

  3. "TH"

    Returns: 37

  4. "HT"

    Returns: 57

  5. "HTH"

    Returns: 13

  6. "TTHTTHTTHTTHTTHTTH"

    Returns: 33

  7. "TTTTTTTTTTTTTTTTTTTH"

    Returns: 22

  8. "TTTTTTTTTTH"

    Returns: 212

  9. "HHHTHHHHHHTTTHHHHHHHHHTTHHT"

    Returns: 328

  10. "THTHT"

    Returns: -1

  11. "HTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"

    Returns: 944

  12. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTH"

    Returns: 934

  13. "THH"

    Returns: 109

  14. "HHHTHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH"

    Returns: 1184

  15. "T"

    Returns: -1

  16. "HTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTH"

    Returns: 103

  17. "HHTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"

    Returns: 95

  18. "TTTTTTTTT"

    Returns: -1


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: