Slot Game

Posted: 6 Mar, 2021
Difficulty: Easy

PROBLEM STATEMENT

Try Problem

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
Approach 1

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

Try Problem