TopcoderArchiveVisit Topcoder
SRM7 Nov 2002
SRM 119

CoinFlip

Problem statement, definition, constraints, and public examples.

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. "TTTHH"
    Returns: 4
Back to all problems