Last Updated: 6 Mar, 2021

Slot Game

Easy
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.
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

Approaches

01 Approach

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'.

02 Approach

We will create a frequency array that will store how many times each colour exists in the original string, excluding those times when there was a perfect hit.  

 

Below is the detailed algorithm:

 

  1. Make a helper function slotScoreHelper(c), where ‘C’ is the color, it will return an integer depending on the current slot color.
    • If the current character is ‘B’, return 0.
    • If the current character is ‘G’, return 1.
    • If the current character is ‘R’, return 2.
    • If the current character is ‘Y’, return 3.
  2. Make a frequency array 'COUNT' to store each character’s occurrences in the original string and an integer 'POINTS' to store the points scored.
  3. Run a loop from(loop variable ‘i’) from 0 till 4 to calculate the perfect hits.
    • If 'ORIGINAL[i]' = 'GUESS[i]', we increment points by two since it’s a perfect hit.
    • Else, we make an integer variable 'COLOUR' that will hold a value depending on the original string’s current character, i.e 'COLOUR' = slotScoreHelper('ORIGINAL[i]').
    • Increment 'COUNT[COLOUR]' by 1 to update the count of this current 'COLOUR' in the frequency array 'COUNT'.
  4. Run a loop from(loop variable ‘i’) from 0 till 4 to calculate the pseudo hits.
    • Similar to step 2(b), make an integer variable 'COLOUR', which will hold a value depending on the current character of the guess string, i.e., 'COLOUR' = slotScoreHelepr('GUESS[i]').
    • If the current 'COLOUR' of our guess doesn’t matches with the current 'COLOUR' of the original, and 'COUNT[COLOUR]' > 0, i.e., the occurrence of this 'COLOUR' is greater than 0, then increment 'POINTS' by one and decrement 'COUNT[COLOUR]' by 1 to mark that this 'COLOUR' has been considered once.
  5. Return 'POINTS'.