Statistics

Problem Statement for "CoinFlip"

Problem Statement

Harry and Sally want to rent a movie. Harry wants to watch "The Terminator", and Sally wants to watch "When Harry Met Sally". They decide to flip a coin. Unfortunately, they have only one coin, which is bent, so they think that it is not a fair coin (the chances of heads and tails may not be equal).

I advise them to use the following procedure, which makes the probability of either outcome equal: Flip the coin twice. If it flips heads the first time and tails the second time, then they watch "The Terminator"; if it flips tails the first time and heads the second time, then they watch "When Harry Met Sally". If it flips the same (heads or tails) both times, they repeat the procedure (they flip twice again).

Your task is, given a String representing the sequence of flip results, return the number of the flip (the flips are 1-indexed) after which they can choose a movie according to the protocol described above. If they can't choose return -1.

Definition

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

Constraints

  • flips has between 0 and 50 characters inclusive
  • each character of flips is either an 'H', representing heads, or a 'T', representing tails.

Examples

  1. ""

    Returns: -1

  2. "HHTH"

    Returns: 4

  3. "TTHHTHHHTH"

    Returns: 6

  4. "TTHH"

    Returns: -1

  5. "TT"

    Returns: -1

  6. "HH"

    Returns: -1

  7. "TH"

    Returns: 2

  8. "HT"

    Returns: 2

  9. "HHHHHHHHHHTTTTTTTTTTHHHHHHHHHHTTTTTTTTTTHHHHHHHHHH"

    Returns: -1

  10. "HTHTHTHTHTHT"

    Returns: 2

  11. "HHTTHHTTHHTTHHTTHHTT"

    Returns: -1

  12. "HHTTHHTTHHTTHHTTHHTTTHHHTTHHTTHHTTHHTTHHTT"

    Returns: 22

  13. "HHTTHTHHHH"

    Returns: 6

  14. "TTHHTTTTTTTTHT"

    Returns: 14

  15. "TTTTTTTTTTTTHTTTTTTTTTTT"

    Returns: 14

  16. "T"

    Returns: -1

  17. "H"

    Returns: -1

  18. "TTT"

    Returns: -1

  19. "THH"

    Returns: 2

  20. "TTTTTTH"

    Returns: -1

  21. "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH"

    Returns: -1

  22. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"

    Returns: -1

  23. "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHT"

    Returns: -1

  24. "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHT"

    Returns: 50

  25. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTH"

    Returns: 50

  26. "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTH"

    Returns: -1

  27. "TTTHH"

    Returns: 4

  28. "T"

    Returns: -1

  29. "HH"

    Returns: -1

  30. "TTHHT"

    Returns: -1

  31. "TT"

    Returns: -1

  32. "HHH"

    Returns: -1

  33. ""

    Returns: -1

  34. "H"

    Returns: -1

  35. "TTH"

    Returns: -1

  36. "HHHHH"

    Returns: -1

  37. "HHTTH"

    Returns: -1

  38. "HHT"

    Returns: -1

  39. "TTTHH"

    Returns: 4

  40. "HT"

    Returns: 2

  41. "TTT"

    Returns: -1

  42. "TTHHTTHH"

    Returns: -1

  43. "HTTH"

    Returns: 2

  44. "HTH"

    Returns: 2


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: