# RGYB(color) Slots Game to guess the correct color for the correct slot

Last Updated : 17 May, 2023

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.

Examples:

```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:

## C++

 `// CPP code to solve RGYB game` `#include` `#include` `using` `namespace` `std;` `int` `hits = 0;` `int` `pseudoHits = 0;`   `// maximum no of colors are 4 RGYB` `int` `MAX_COLORS = 4;`   `// Function to assign code to a slot` `// based on the color they have` `int` `codeOfColor(``char` `c)` `{` `    ``switch` `(c) ` `        ``{` `        ``case` `'B'``: ``return` `0;` `            ``case` `'G'``: ``return` `1;` `            ``case` `'R'``: ``return` `2;` `            ``case` `'Y'``: ``return` `3;` `            ``default``:` `            ``return` `-1;` `        ``}` `}`   `// Function to calculate number of hits` `// and pseudo hits` `void` `calcResult(string guess, string solution)` `{` `    ``hits = 0;` `    ``pseudoHits = 0;` `    `  `    ``if``(guess.length() != solution.length())` `        ``cout<<``"Incorrect Input"``<= 0 && frequencies[codecolor] > 0` `                               ``&& guess[i] != solution[i]) ` `            ``{` `                ``pseudoHits++;` `                ``frequencies[codecolor]--;` `            ``}` `        ``}` `        `  `    ``cout << ``"hits -> "` `<< hits << ``" Pseudo hits -> "` `                       ``<< pseudoHits << endl;` `}`   `// Driver code to test above function` `int` `main()` `{` `    ``string solution = ``"GGRR"``;` `    ``string guess = ``"RGBY"``;` `         `  `    ``calcResult(solution, guess);` `         `  `    ``solution = ``"YGRR"``;` `    ``guess = ``"RGYB"``;` `         `  `    ``calcResult(solution, guess);` `}` `// This code is contributed by Sumit Ghosh`

## Java

 `// Java code to solve RGYB game`   `import` `java.util.*;` `import` `java.lang.*;` `import` `java.io.*;`   `class` `CalcRes ` `{` `    ``static` `int` `hits = ``0``;` `    ``static` `int` `pseudoHits = ``0``;` `    `  `    ``// Function to assign code to a slot` `    ``// based on the color they have` `    ``static` `int` `codeOfColor(``char` `c)` `    ``{` `        ``switch` `(c) ` `        ``{` `        ``case` `'B'``: ``return` `0``;` `            ``case` `'G'``: ``return` `1``;` `            ``case` `'R'``: ``return` `2``;` `            ``case` `'Y'``: ``return` `3``;` `            ``default``:` `            ``return` `-``1``;` `        ``}` `    ``}` `    `  `    ``// maximum no of colors are 4 RGYB` `    ``static` `int` `MAX_COLORS = ``4``;` `    `  `    ``// Function to calculate number of hits` `    ``// and pseudo hits` `    ``static` `void` `calcResult(String guess, String solution)` `    ``{   ` `        ``hits = ``0``;` `        ``pseudoHits = ``0``;` `        `  `        ``if` `(guess.length() != solution.length())` `            ``System.out.println(``"Incorrect Input"``);` `        `  `        ``// frequency array to store how many times ` `        ``// each character occurs in solution string` `        ``int``[] frequencies = ``new` `int``[MAX_COLORS];`   `        ``// Compute hits and build frequency table ` `        ``for` `(``int` `i = ``0``; i < guess.length(); i++) ` `        ``{` `            ``if` `(guess.charAt(i) == solution.charAt(i)) ` `            ``{` `                ``hits++;` `            ``} ` `            ``else` `            ``{` `                ``/* ` `                   ``Only increment the frequency table (which will be ` `                   ``used for pseudo-hits) if it's not a hit. If it's a ` `                   ``hit, the slot has already been "used." ` `                ``*/` `                ``int` `codeofcolor = codeOfColor(solution.charAt(i));` `                ``frequencies[codeofcolor]++;` `            ``}` `        ``}`   `        ``// Compute pseudo-hits ` `        ``for` `(``int` `i = ``0``; i < guess.length(); i++) ` `        ``{` `            ``int` `codeofcolor = codeOfColor(guess.charAt(i));` `            ``if` `(codeofcolor >= ``0` `&& frequencies[codeofcolor] > ``0` `&& ` `                              ``guess.charAt(i) != solution.charAt(i)) ` `            ``{` `                ``pseudoHits++;` `                ``frequencies[codeofcolor]--;` `            ``}` `        ``}` `        `  `        ``System.out.println(``"hits -> "` `+ hits + ` `                           ``" Pseudo hits -> "` `+ pseudoHits);` `    ``}`     `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args)` `    ``{   ` `        ``String solution = ``"GGRR"``;` `        ``String guess = ``"RGBY"``;` `        `  `        ``calcResult(solution, guess);` `        `  `        ``solution = ``"YGRR"``;` `        ``guess = ``"RGYB"``;` `        `  `        ``calcResult(solution, guess);` `    ``}` `}`

## Python3

 `# Python code to solve RGYB game`   `hits ``=` `0` `pseudoHits ``=` `0`   `# maximum no of colors are 4 RGYB` `MAX_COLORS ``=` `4`   `# Function to assign code to a slot` `# based on the color they have` `def` `codeOfColor(c):` `    ``if` `c ``=``=` `'B'``:` `        ``return` `0``;` `    ``elif` `c ``=``=` `'G'``:` `        ``return` `1``;` `    ``elif` `c ``=``=` `'R'``:` `        ``return` `2``;` `    ``elif` `c ``=``=` `'Y'``:` `        ``return` `3``;` `    ``else``:` `        ``return` `-``1``;`   `# Function to calculate number of hits` `# and pseudo hits` `def` `calcResult(guess, solution):` `    ``hits ``=` `0` `    ``pseudoHits ``=` `0` `    ``if` `(``len``(guess) !``=` `len``(solution)):` `        ``print` `(``"Incorrect Input"``)` `    `  `    ``# frequency array to store how many times` `    ``# each character occurs in solution string` `    ``frequencies ``=` `[``0` `for` `i ``in` `range``(MAX_COLORS)]` `    `  `    ``# Compute hits and build frequency table` `    ``for` `i ``in` `range``(``len``(guess)):` `        ``if` `(guess[i] ``=``=` `solution[i]):` `            ``hits ``+``=` `1` `        ``else``:`   `            ``# Only increment the frequency table (which` `            ``# will be used for pseudo-hits) if it's not` `            ``# a hit. If it's a hit, the slot has already` `            ``# been "used."` `            ``codecolor ``=` `codeOfColor(solution[i])` `            ``frequencies[codecolor] ``+``=` `1`   `    ``# Compute pseudo-hits` `    ``for` `i ``in` `range``(``len``(guess)):` `        ``codecolor ``=` `codeOfColor(guess[i])` `        ``if` `codecolor >``=` `0` `and` `frequencies[codecolor] > ``0` `and` `guess[i] !``=` `solution[i]:` `                ``pseudoHits ``+``=` `1` `                ``frequencies[codecolor] ``-``=` `1` `    ``print``(``"hits -> "``, hits, ``" Pseudo hits -> "``, pseudoHits)`   `# Driver code to test above function` `solution ``=` `"GGRR"` `guess ``=` `"RGBY"` `calcResult(solution, guess)`   `solution ``=` `"YGRR"` `guess ``=` `"RGYB"` `calcResult(solution, guess)`   `#This code is contributed by Sachin Bisht`

## C#

 `// C# code to solve RGYB game` `using` `System;`   `class` `GFG {` `    `  `    ``static` `int` `hits = 0;` `    ``static` `int` `pseudoHits = 0;` `    `  `    ``// Function to assign code to a slot` `    ``// based on the color they have` `    ``static` `int` `codeOfColor(``char` `c)` `    ``{` `        ``switch` `(c) ` `        ``{` `            ``case` `'B'``: ``return` `0;` `            ``case` `'G'``: ``return` `1;` `            ``case` `'R'``: ``return` `2;` `            ``case` `'Y'``: ``return` `3;` `            ``default``: ``return` `-1;` `        ``}` `    ``}` `    `  `    ``// maximum no of colors are 4 RGYB` `    ``static` `int` `MAX_COLORS = 4;` `    `  `    ``// Function to calculate number of hits` `    ``// and pseudo hits` `    ``static` `void` `calcResult(``string` `guess, ``string` `solution)` `    ``{ ` `        ``hits = 0;` `        ``pseudoHits = 0;` `        `  `        ``if` `(guess.Length != solution.Length)` `            ``Console.Write(``"Incorrect Input"``);` `        `  `        ``// frequency array to store how many` `        ``// times each character occurs in` `        ``// solution string` `        ``int` `[]frequencies = ``new` `int``[MAX_COLORS];`   `        ``// Compute hits and build frequency table ` `        ``for` `(``int` `i = 0; i < guess.Length; i++) ` `        ``{` `            ``if` `(guess[i] == solution[i]) ` `            ``{` `                ``hits++;` `            ``} ` `            ``else` `            ``{` `                `  `                ``/* Only increment the frequency table` `                ``(which will be used for pseudo-hits)` `                ``if it's not a hit. If it's a hit, the` `                ``slot has already been "used." */` `                ``int` `codeofcolor = codeOfColor(solution[i]);` `                ``frequencies[codeofcolor]++;` `            ``}` `        ``}`   `        ``// Compute pseudo-hits ` `        ``for` `(``int` `i = 0; i < guess.Length; i++) ` `        ``{` `            ``int` `codeofcolor = codeOfColor(guess[i]);` `            ``if` `(codeofcolor >= 0 && ` `                        ``frequencies[codeofcolor] > 0 && ` `                                ``guess[i] != solution[i]) ` `            ``{` `                ``pseudoHits++;` `                ``frequencies[codeofcolor]--;` `            ``}` `        ``}` `        `  `        ``Console.WriteLine(``"hits -> "` `+ hits + ` `                     ``" Pseudo hits -> "` `+ pseudoHits);` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `Main()` `    ``{ ` `        ``string` `solution = ``"GGRR"``;` `        ``string` `guess = ``"RGBY"``;` `        `  `        ``calcResult(solution, guess);` `        `  `        ``solution = ``"YGRR"``;` `        ``guess = ``"RGYB"``;` `        `  `        ``calcResult(solution, guess);` `    ``}` `}`   `// This code is contributed by nitin mittal.`

## Javascript

 ``

Output

```hits -> 1 Pseudo hits -> 1
hits -> 1 Pseudo hits -> 2```

Time Complexity: O(N)
Auxiliary Space: O(N)