Last Updated: 7 Jul, 2022

Rock Paper Scissor

Easy
Asked in companies
MicrosoftGoldman Sachs

Problem statement

Nezuko and Zenitsu were playing Rock Paper Scissors which is a very popular game where each player can do three moves which are: ‘Rock’, ‘Paper’ and ‘Scissor’.

The winner of a game is decided in the following way:

‘Rock’ beats ‘Scissor’.

‘Scissor’ beats ‘Paper’.

‘Paper’ beats ‘Rock’.

If both players make the same move like ‘Rock’ is made by both person then that result in a draw and whose point will not be rewarded to anyone.

Now you have been provided with two strings representing the moves that they will make for ‘NEZUKO’ represents Nezuko’s string and ‘ZENITSU’ represents Zenitsu’s moves.

In the string ‘R’ represents ‘ROCK’ and ‘P’ represents ‘PAPER’ and ‘S’ represents ‘SCISSORS’.

There is a total of ‘K’ games that will be played and the player will play the move by traversing the string provided. If a player reaches the end of the string then his next moves will be played from the start of the string.

You have to tell the number of games won by Nezuko and the number of games won by Zenitsu respectively in order.

Example:
Input: ‘K’ = 3, ‘NEZUKO’ = ‘RP’, ‘ZENITSU’ = ‘R’
Output: 1 0
Game 1: ‘NEZUKO’ = ‘R’, ‘ZENITSU’ =  ‘R’, Result = ‘Draw’.
Game 2: ‘NEZUKO’ = ‘P’, ‘ZENITSU’ = ‘R’, Result = ‘Nezuko won the game’.
Game 3: ‘NEZUKO’ = ‘R’, ‘ZENITSU’ =  ‘R’, Result = ‘Draw’.
Input Format:
The first line of the input contains a single integer 'T', representing the number of test cases.
For each test case:
The first line will contain the value ‘K’.
The second line will contain the string ‘NEZUKO’ representing the moves of ‘Nezuko’.
The third line will contain the string ‘ZENITSU’ representing the moves of ‘Zenitsu’.
Output format:
For each test case, print the number of games won by Nezuko and the number of games won by Zenitsu respectively in order.
Note:
You don’t need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10 
1 <= K <= 10^5
1<= |NEZUKO| <= 10^5
1<= |ZENITSU| <= 10^5
Strings contain only 3 types of values ‘R’, ‘P’ and ‘S’.
Time Limit: 1 sec

Approaches

01 Approach

Considering ‘N’ represents the size of string ‘nezuko’ and ‘Z’ represents size of string ‘zenitsu’ we will start traversing the ‘i’ from 0 to ‘K’ - 1 the move made by ‘NEZUKO’ at ‘i’th game will be nezuko[ i % N] and move made by ‘ZENITSU’ at ‘i’th game will be zenitsu[ i % Z ] and we will maintain two counts, one for ‘NEZUKO’ and one for ‘ZENITSU’ and we will increase the count of respective winner.
 

The Steps are as follows:

rockPaperScissor( nezuko, zenitsu, k):

  1. Create a variable ‘N’ representing the size of string ‘nezuko’ and a variable ‘z’ representing the size of string ‘zenitsu’.
  2. Create two count variables namely ‘nezukoCount’ and ‘zenitsuCount’ intialized with 0.
  3. For ‘i’ in range[0, k-1]:
    1. Check the winner of ‘nezuko[ i %N ]’ and ‘zenitsu[ i%Z ]’ and increase the respective count of the winner if there is any winner.
  4. Return the count of both of the players.