Rock Paper Scissor

Easy
0/40
Average time to solve is 15m
profile
Contributed by
5 upvotes
Asked in companies
MicrosoftGoldman Sachs

Problem statement

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’.
Detailed explanation ( Input/output format, Notes, Images )
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
Sample Input 1 :
2
3
RP
R
4
PRSP
RPSR
Sample Output 1 :
1 0
2 1
Explanation Of Sample Input 1 :
For test case 1: 
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’.

For test case 2:
Game 1: ‘NEZUKO’ = ‘P’, ‘ZENITSU’ = ‘R’, Result = ‘Nezuko won the game’.
Game 2: ‘NEZUKO’ = ‘R’, ‘ZENITSU’ = ‘P’, Result = ‘Zenitsu won the game’.
Game 3: ‘NEZUKO’ = ‘S’, ‘ZENITSU’ = ‘S’, Result = ‘Draw’.
Game 4: ‘NEZUKO’ = ‘P’, ‘ZENITSU’ = ‘R’, Result = ‘Nezuko won the game’.
Sample Input 2 :
2
4
RPSPSRPSR
SPRPSRPPRSR
6
RPSPRSPS
RPS
Sample Output 2 :
1 1
1 1
Hint

Check for the result of each game and add the result to the respective winner.

Approaches (1)
Implementation

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.
Time Complexity

O( K ), Where K represents the number of games to be played.

 

We are traversing a loop of size ‘K’

Hence time complexity is O( K ).

Space Complexity

O( 1 ), where ‘1’ is constant.

 

We are not using any extra space except for some variables which takes constant time.

Hence, the overall Space Complexity is O( 1 ).

Code Solution
(100% EXP penalty)
Rock Paper Scissor
Full screen
Console