TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

RockPaper

Problem statement, definition, constraints, and public examples.

Problem Statement

You are playing Rock-Paper-Scissor with your friend. It is a game that works as follows:

  • 1) You say aloud Rock...Paper...Scissor...
  • 2) Then you make your hand visible to your opponent in the shape of a either a rock, paper, or scissor
  • 3) Rock beats Scissor, Paper beats Rock, Scissor beats Paper, and all other combinations are draws
You have decided to make a deal with your friend. You will tell your friend your entire sequence of moves before playing if he agrees to always make moves in triplets. This means if he chooses Rock, he has to play Rock three times in a row. Then he can rechoose for the next 3 moves. Given a list of moves, determine the maximum number of times your friend can win. For example: moves = "RRRPPPSSS" (R = Rock, P = Paper, S = Scissor) Then he can win all 9 times by playing "PPPSSSRRR".

Create a class RockPaper that contains the method maxWins, which takes a String moves and returns an int that represents the maximum number of times your friend can win.

Definition

Class:
RockPaper
Method:
maxWins
Parameters:
String
Returns:
int
Method signature:
int maxWins(String moves)
(be sure your method is public)

Constraints

  • moves will have length between 3 and 48 inclusive
  • moves will only contain the characters 'R','P', and 'S' (quotes for clarity)
  • moves will have length divisible by 3

Examples

  1. "RRRPPPSSS"
    Returns: 9
    This is the example stated above.
  2. "RPSRPSRPS"
    Returns: 3
    Regardless of what moves your opponent makes he will always win 3 times.
  3. "RRRRPPPPS"
    Returns: 7
    If your opponent plays "PPPSSSSSS" he will beat you 7 times.
  4. "SSRRPPSSRRPP"
    Returns: 8
  5. "SRPRRRPSSSSPSSS"
    Returns: 11
Back to all problems