Cheating Codemaker
Learn this problemProblem statement
A secret code is a four-character string. Each character is one of R, O, Y, G, B, or P, and colors may repeat.
For a guess and a secret code, the response contains two integers:
- Exact matches: positions whose colors match exactly.
- Color matches: remaining colors that occur in both strings but at different positions.
Every character can contribute to at most one match. Count exact matches first, then match the remaining characters by color.
You are the codemaker, but you do not commit to one secret code at the beginning. A history of earlier guesses and responses is provided. A secret code is currently possible if its response to every earlier guess equals the corresponding earlier response.
Given the next guess, return the response that leaves the largest number of currently possible secret codes. In other words, group all currently possible codes by the response they would produce for guess and choose the largest group.
If several responses leave the same number of codes, choose the response with more exact matches. If there is still a tie, choose the response with more color matches.
Return [exactMatches, colorMatches].
Function
chooseResponse(guess: String, previousGuesses: List<String>, previousResponses: List<List<Integer>>) → int[]Examples
Example 1
guess = "RGBY"previousGuesses = []previousResponses = []return = [0,2]All 1,296 codes are initially possible. The response [0,2] is produced by 312 of them, more than any other response.
Example 2
guess = "GPPP"previousGuesses = ["RGBY","PPPP"]previousResponses = [[0,0],[3,0]]return = [2,1]The history leaves exactly GPPP, PGPP, PPGP, and PPPG. Three produce [2,1] for the new guess, while one produces [3,0].
Example 3
guess = "YYYY"previousGuesses = ["PPPP"]previousResponses = [[0,0]]return = [1,0]Responses [0,0] and [1,0] each leave 256 codes. The exact-match tie-break selects [1,0].
Example 4
guess = "RGBY"previousGuesses = ["RGBY"]previousResponses = [[4,0]]return = [4,0]The history identifies RGBY as the only possible code, so the response is forced.
Constraints
guess.length() == 4.- Every guess contains only
R,O,Y,G,B, andP. previousGuesses.size() == previousResponses.size().- Every earlier guess has length 4.
- Every earlier response contains two integers in the order
[exactMatches, colorMatches]. - The provided history is well-formed, internally consistent, and leaves at least one possible secret code.