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”.
.png)
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.
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.
1 <= T <= 5
length(original) = length(guess) = 4
Time limit: 1 sec
1
RGBY
GGRR
3
.png)
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.
1
RGYB
YGRR
4
.png)
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.
Traverse both the strings and check each character simultaneously.
We will check for each slot, whether it’s a perfect hit or a pseudo hit.
Below is the detailed algorithm:
Return 'POINTS'.
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).
O(1).
As we are not using any extra space.