299. Bulls and Cows
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to implement the logic for the game "Bulls and Cows". Given a secret number and a guessed number, we need to provide a hint. The hint consists of:
Solution
Bulls and Cows in Python
class Solution:
def getHint(self, secret: str, guess: str) -> str:
bulls = 0
cows = 0
# counts array to store the balance of digits.
# Positive value means secret has an excess of this digit.
# Negative value means guess has an excess of this digit.
counts = [0] * 10
for i in range(len(secret)):
s_char = secret[i]
g_char = guess[i]
if s_char == g_char:
# It's a bull
bulls += 1
else:
# It's not a bull, might be a cow.
s_digit = int(s_char)
g_digit = int(g_char)
# Check if the current secret digit can match a previous guess digit
# If counts[s_digit] < 0, it means guess has a pending digit s_digit
if counts[s_digit] < 0:
cows += 1
# Record the secret digit
counts[s_digit] += 1
# Check if the current guess digit can match a previous secret digit
# If counts[g_digit] > 0, it means secret has a pending digit g_digit
if counts[g_digit] > 0:
cows += 1
# Record the guess digit
counts[g_digit] -= 1
return f"{bulls}A{cows}B"
Bulls and Cows in Java
class Solution {
public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
// counts array to store the balance of digits.
// Positive value means secret has an excess of this digit.
// Negative value means guess has an excess of this digit.
int[] counts = new int[10];
for (int i = 0; i < secret.length(); i++) {
char s_char = secret.charAt(i);
char g_char = guess.charAt(i);
if (s_char == g_char) {
// It's a bull
bulls++;
} else {
// It's not a bull, might be a cow.
int s_digit = s_char - '0';
int g_digit = g_char - '0';
// Check if the current secret digit can match a previous guess digit
// If counts[s_digit] < 0, it means guess has a pending digit s_digit
if (counts[s_digit] < 0) {
cows++;
}
// Record the secret digit
counts[s_digit]++;
// Check if the current guess digit can match a previous secret digit
// If counts[g_digit] > 0, it means secret has a pending digit g_digit
if (counts[g_digit] > 0) {
cows++;
}
// Record the guess digit
counts[g_digit]--;
}
}
return bulls + "A" + cows + "B";
}
}
Bulls and Cows in C++
class Solution {
public:
string getHint(string secret, string guess) {
int bulls = 0;
int cows = 0;
// counts array to store the balance of digits.
// Positive value means secret has an excess of this digit.
// Negative value means guess has an excess of this digit.
vector<int> counts(10, 0);
for (int i = 0; i < secret.length(); ++i) {
char s_char = secret[i];
char g_char = guess[i];
if (s_char == g_char) {
// It's a bull
bulls++;
} else {
// It's not a bull, might be a cow.
int s_digit = s_char - '0';
int g_digit = g_char - '0';
// Check if the current secret digit can match a previous guess digit
// If counts[s_digit] < 0, it means guess has a pending digit s_digit
if (counts[s_digit] < 0) {
cows++;
}
// Record the secret digit
counts[s_digit]++;
// Check if the current guess digit can match a previous secret digit
// If counts[g_digit] > 0, it means secret has a pending digit g_digit
if (counts[g_digit] > 0) {
cows++;
}
// Record the guess digit
counts[g_digit]--;
}
}
return to_string(bulls) + "A" + to_string(cows) + "B";
}
};
Bulls and Cows in TypeScript
function getHint(secret: string, guess: string): string {
let bulls = 0;
let cows = 0;
// counts array to store the balance of digits.
// Positive value means secret has an excess of this digit.
// Negative value means guess has an excess of this digit.
const counts: number[] = new Array(10).fill(0);
for (let i = 0; i < secret.length; i++) {
const s_char = secret[i];
const g_char = guess[i];
if (s_char === g_char) {
// It's a bull
bulls++;
} else {
// It's not a bull, might be a cow.
const s_digit = parseInt(s_char);
const g_digit = parseInt(g_char);
// Check if the current secret digit can match a previous guess digit
// If counts[s_digit] < 0, it means guess has a pending digit s_digit
if (counts[s_digit] < 0) {
cows++;
}
// Record the secret digit
counts[s_digit]++;
// Check if the current guess digit can match a previous secret digit
// If counts[g_digit] > 0, it means secret has a pending digit g_digit
if (counts[g_digit] > 0) {
cows++;
}
// Record the guess digit
counts[g_digit]--;
}
}
return `${bulls}A${cows}B`;
};
More Medium LeetCode answers
- 99. Recover Binary Search Tree
- 102. Binary Tree Level Order Traversal
- 103. Binary Tree Zigzag Level Order Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 107. Binary Tree Level Order Traversal II
- 113. Path Sum II
- 114. Flatten Binary Tree to Linked List
- 116. Populating Next Right Pointers in Each Node
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.