96.9k views
1 vote
Franco is going to play a game of Rock-Paper-Scissors with his friend Giovanni. In each turn, both players make their chosen gesture (rock, paper or scissors) simultaneously. After every turn, players gain points as follows: 2 for a win (rock beats scissors, scissors beat paper and paper beats rock), 1 for a tie (when both players show the same gesture) and 0 for a loss.

Franco wants to surprise Giovanni by using a very simple strategy: he will make the same gesture in every turn throughout the game. What is the maximum number of points he can score using this strategy?

Write a function:

class Solution { public int solution(String G); }

that, given a string G representing the sequence of Giovanni's turns ('R' represents a rock, 'P' represents paper and 'S' represents scissors), returns the maximum number of points Franco can score using his strategy.

For example:

1. Given "RSPRS", the function should return 6 (with the chosen gesture being rock). Franco will gain 6 points (he will win in the second and fifth turns and tie in the first and fourth turn).

2. Given "SRR" the function should return 4 (chosen gesture: rock; he will win in the first turn and tie in the second and third turns).

3. Given "PRPRRPP" the function should return 10 (chosen gesture: paper).

4. Given "PPPPRRSSSSS" the function should return 13 (chosen gesture: scissors).


Assume that:

1. the length of string G is within the range [1..100];
2. string G consists only of letters 'R', 'P', and 'S'.


.In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

User Macaroni
by
7.3k points

1 Answer

1 vote

Below is the usage of the solution() function:

java

class Solution {

public int solution(String G) {

int len = G.length();

int maxPoints = 0;

// Try each gesture (rock, paper, scissors) as Franco's chosen gesture

for (char gesture : "RPS".toCharArray()) {

int points = 0;

// Calculate points for each turn based on Franco's chosen gesture

for (int i = 0; i < len; i++) {

char giovanniGesture = G.charAt(i);

if (gesture == giovanniGesture) {

// Franco and Giovanni tied

points += 1;

} else if ((gesture == 'R' && giovanniGesture == 'S') ||

(gesture == 'P' && giovanniGesture == 'R') ||

(gesture == 'S' && giovanniGesture == 'P')) {

// Franco wins

points += 2;

}

// If Giovanni wins, no points are added

}

// Update maxPoints if the current gesture yields more points

maxPoints = Math.max(maxPoints, points);

}

return maxPoints;

}

}

What is the code function?

The Java function is one that tends to iterates through possible gestures (rock, paper, scissors) as Franco's choice, calculating points for each turn against Giovanni. Franco earns 2 points for winning and 1 for tying.

So, The maximum points achievable across all gestures tells more on the optimal strategy. The solution ensures correctness within the given constraints, with a focus on clarity rather than performance.

User Wiktor Kozlik
by
8.4k points