Given that you have four slots, and each slot will contain a color red (R), yellow (Y), green (G), blue (B) respectively. For example, if you select YGGR (Slot-1 is yellow, Slots-2 and -3 are green, Slot -4 is red). The colors of slots are not known to you beforehand. You will make a guess about the colors. You might, for example, guess YRGB.
When you guess the correct color for the correct slot, you get a “hit” If you guess a color that exists but is in the wrong slot, you get a “pseudo-hit:’ Note that a slot that is a hit can never count as a pseudo-hit.
Given a guess and a solution, your task is to write a program to calculate the number of hits and pseudo-hits.
Input: solution -> RGBY, Guess -> GGRR Output: hit -> 1, pseudohit -> 1 Input: solution -> RGYB, Guess -> YGRR Output: hit -> 1, pseudohit -> 2 Input: solution -> RGYB, Guess -> GGYR Output: hit -> 2, pseudohit -> 1
A simple solution will be to traverse both strings simultaneously and check the characters of both strings. If both strings have same character then it is a hit and thus increment the count of hits. If the characters of strings do not match at a position, then traverse the solution string again to see if the character in guess is occurred anywhere in solution string or not, if yes increment count of pseudo hits.
Time Complexity: O(N*N) , where N is the length of string.
An efficient solution is to create a frequency array which stores how many times each character occurs in solution, excluding times when the slot is a “hit”. Then, we iterate through guess to count the number of pseudo-hits.
Below is the implementation of above approach:
hits -> 1 Pseudo hits -> 1 hits -> 1 Pseudo hits -> 2
Time Complexity: O(N)
Auxiliary Space: O(N)
This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Game of replacing array elements
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Coin game of two corners (Greedy Approach)
- Find out the correct position of the ball after shuffling
- Count the numbers that can be reduced to zero or less in a game
- Number of ways for playing first move optimally in a NIM game
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Optimal Strategy for a Game | Set 2
- Optimal Strategy for a Game | Set 3
- Color all boxes in line such that every M consecutive boxes are unique
- Count of elements which are not at the correct position
- Find the winner of the Game
- Minimum colors required such that edges forming cycle do not have same color
- Two player game in which a player can remove all occurrences of a number
- Minimum number of colors required to color a Circular Array
- Optimal Strategy for the Divisor game using Dynamic Programming
- Number of ways to color N-K blocks using given operation