Slot Game

Easy
0/40
Average time to solve is 10m
profile
Contributed by
55 upvotes
Asked in companies
Goldman SachsSoft SuaveGoogle inc

Problem statement

You’re given a slot machine with four slots where each slot will contain the color Red(R), Yellow(Y), Green(G), Blue(B), respectively. You don’t know the colors of this slot beforehand. You have to guess the colors. When you guess the correct color for the correct slot, you get a perfect hit, and you get 2 points, but if you guess a color that exists in the machine but is in the wrong slot, you get a pseudo hit, and you get 1 point.

You’re given the original string representing the slots’ colors and the guess string, and your task is to calculate and return the total number of points you have scored.

Note:

A slot that has been counted as a perfect hit can never count as a pseudo-hit.

Example:

Original String = “RGYB” and Guess String = “YGRR”.

Alt text

The second slot of both the guess and original matches, hence it’s a perfect hit. The guess string contains yellow, which is also present in the original string but not at the same slot as the guess slot. Hence it’s a pseudo hit. The guess string also contains two red slots, but the original string contains only one red which is also not at the same slot as the guess string; hence it is also a pseudo hit. Therefore total points will be 2+1+1= 4.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains an integer T denoting the number of test cases.

The first line of each test contains a string ‘original’, representing the original color of four slots.

The second line of each test case contains a string, ‘guess,’ representing the guessed color of four slots.
Output Format:
For every test case, print the total number of points that you have scored.
Note :
You do not need to print anything; it has already been taken care of. Just implement the given function. 
Constraints:
1 <= T <= 5
length(original) = length(guess) = 4

Time limit: 1 sec
Sample Input 1
1
RGBY
GGRR
Sample Output 1:
3
Explanation of Sample Input 1:

Alt text

The second slot of both the string matches. Therefore, it is a perfect hit, and we get 2 points. Our guess string contains the color Red ‘R’ two times, which is also present in the original string but only one time, and it is not in the same slot as the guess slot; hence we get a pseudo hit we get 1 point. Therefore total points will be 2+1=3.
Sample Input 2:
1
RGYB
YGRR
Sample Output 2:
4
Explanation of Sample Input 2:

Alt text

The second slot of both the string matches. Therefore it is a perfect hit, and we get 2 points. Our guess strings also contain the color Yellow ‘Y’, which is also present in the original slot, but it is not in the same slot as the guess string; hence we get a pseudo hit, and we get 1 point. Similarly, Our guess strings also contain the color  Red ‘R’ two times, which is also present in the original slot but only once, and it is not in the same slot as the guess string; hence we get a pseudo hit and get 1 point. Hence total points will be 2+1+1=4.
Hint

Traverse both the strings and check each character simultaneously.

Approaches (2)
Brute Force

We will check for each slot, whether it’s a perfect hit or a pseudo hit.

 

Below is the detailed algorithm:
 

  1. Maintain an integer variable 'POINTS' to store the points scored.
  2. Run a loop from(loop variable ‘i’) from 0 till 4 to calculate the perfect hits.
    1. If 'ORIGINAL[i]' = 'GUESS[i]', we increment 'POINTS' by two since it’s a perfect hit, and then update both 'ORIGINAL[i]' and 'GUESS[i]' to ‘#’ so that we don’t count them again while calculating pseudo hits.
  3. Run a loop from(loop variable ‘i’) from 0 till 4 to calculate the pseudo hits.
    1. If 'GUESS[i]' is not equal to ‘#,’ then run another loop(loop variable ‘j’) to find if 'GUESS[i]' exists in the original slot or not.
      1. If 'ORIGINAL[j]' = 'GUESS[i]', increment 'POINTS' by 1 and update both 'ORIGINAL[j]' and 'GUESS[i]' to ‘#’ so that we don’t count them again.

Return 'POINTS'.

Time Complexity

O(1).

 

We are traversing the string of length four to count the number of perfect hits and pseudo hits. Hence the final time complexity will be O(4*4) = O(1).

Space Complexity

O(1).

 

As we are not using any extra space.

Code Solution
(100% EXP penalty)
Slot Game
Full screen
Console