The Game of Master Mind is played as follows. The computer chooses a code consisting of four colors from {R, Y, G, B}. A player makes a guess (also four colors). For each guess the player is told:
Input: solution = RGGB, guess = YRGB
Output: (2,1)Explanation:
SOLUTION: R G G B
GUESS: Y R G B
POSITION: 1234Hits are 2 at positions 3 and 4, and pseudo-hits:1(color R exists but in wrong position)
Return the number of hits and pseudo-hits for a given guess and solution.
Note: LeetCode has a very similar problem Bulls and Cows.
Count exact matches (hits) in one pass. For non-matching positions, count remaining colors in the solution and then match them against the guess to compute pseudo-hits.
classSolution {
privateintidx(char c) { return c =='R'? 0 : c =='Y'? 1 : c =='G'? 2 : 3; }
staticclassResult { int hits; int pseudoHits; }
public Result masterMind(String guess, String solution) {
Result res =new Result();
int[] cnt =newint[4];
for (int i = 0; i < guess.length(); i++) {
if (guess.charAt(i) == solution.charAt(i)) {
res.hits++;
} else {
cnt[idx(solution.charAt(i))]++;
}
}
for (int i = 0; i < guess.length(); i++) {
if (guess.charAt(i) == solution.charAt(i)) continue;
int j = idx(guess.charAt(i));
if (cnt[j]> 0) {
cnt[j]--;
res.pseudoHits++;
}
}
return res;
}
}
classSolution:
defmasterMind(self, guess: str, solution: str) -> tuple[int, int]:
defidx(c: str) -> int:
return0if c =='R'else1if c =='Y'else2if c =='G'else3 hits =0 pseudo =0 cnt = [0] *4for i, gch in enumerate(guess):
if gch == solution[i]:
hits +=1else:
cnt[idx(solution[i])] +=1for i, gch in enumerate(guess):
if gch == solution[i]:
continue j = idx(gch)
if cnt[j] >0:
cnt[j] -=1 pseudo +=1return hits, pseudo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
privatefunidx(c: Char) = when (c) { 'R'->0; 'Y'->1; 'G'->2; else->3 }
dataclassResult(var hits: Int = 0, var pseudo: Int = 0)
funmasterMind(guess: String, solution: String): Result {
val cnt = IntArray(4)
val res = Result()
for (i in guess.indices) {
if (guess[i] == solution[i]) res.hits++else cnt[idx(solution[i])]++ }
for (i in guess.indices) {
if (guess[i] == solution[i]) continueval j = idx(guess[i])
if (cnt[j] > 0) { cnt[j]--; res.pseudo++ }
}
return res
}
}
We can compute hits and pseudo-hits in one pass (instead of two) by tracking the net availability of each color. When we encounter a mismatch pair (g,s):
If we previously saw more of g in solution (net count > 0), then using g now creates a pseudo-hit.
If we previously saw more of s in guess (net count < 0), then seeing s on solution side completes a pseudo-hit.
This mirrors the well-known single-pass trick used for Bulls & Cows, specialized to 4 colors. We optionally keep a small bit-mask for presence, though the net counts alone suffice and handle duplicates safely.
classSolution {
public: std::pair<int, int> masterMind(const std::string& guess, const std::string& solution) {
auto idx = [](char c) { return c =='R'?0: c =='Y'?1: c =='G'?2:3; };
int hits =0, pseudo =0;
int net[4] = {0, 0, 0, 0};
for (size_t i =0; i < guess.size(); ++i) {
if (guess[i] == solution[i]) { hits++; continue; }
int g = idx(guess[i]);
int s = idx(solution[i]);
if (net[g] >0) pseudo++; // solution previously had this color unused
if (net[s] <0) pseudo++; // guess previously had this color unused
net[g]--; net[s]++;
}
return {hits, pseudo};
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
privateintidx(char c) { return c =='R'? 0 : c =='Y'? 1 : c =='G'? 2 : 3; }
staticclassResult { int hits; int pseudoHits; }
public Result masterMind(String guess, String solution) {
Result res =new Result();
int[] net =newint[4];
for (int i = 0; i < guess.length(); i++) {
char gch = guess.charAt(i), sch = solution.charAt(i);
if (gch == sch) { res.hits++; continue; }
int g = idx(gch), s = idx(sch);
if (net[g]> 0) res.pseudoHits++;
if (net[s]< 0) res.pseudoHits++;
net[g]--; net[s]++;
}
return res;
}
}