Algorithm
Problem Name: 299. Bulls and Cows
You are playing the Bulls and Cows game with your friend.
You write down a secret number and ask your friend to guess what the number is. When your friend makes a guess, you provide a hint with the following info:
- The number of "bulls", which are digits in the guess that are in the correct position.
- The number of "cows", which are digits in the guess that are in your secret number but are located in the wrong position. Specifically, the non-bull digits in the guess that could be rearranged such that they become bulls.
Given the secret number secret
and your friend's guess guess
, return the hint for your friend's guess.
The hint should be formatted as "xAyB"
, where x
is the number of bulls and y
is the number of cows. Note that both secret
and guess
may contain duplicate digits.
Example 1:
Input: secret = "1807", guess = "7810" Output: "1A3B" Explanation: Bulls are connected with a '|' and cows are underlined: "1807" | "7810"
Example 2:
Input: secret = "1123", guess = "0111" Output: "1A1B" Explanation: Bulls are connected with a '|' and cows are underlined: "1123" "1123" | or | "0111" "0111" Note that only one of the two unmatched 1s is counted as a cow since the non-bull digits can only be rearranged to allow one 1 to be a bull.
Constraints:
1 <= secret.length, guess.length <= 1000
secret.length == guess.length
secret
andguess
consist of digits only.
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define min(a,b) ((a) < (b)?(a):(b))
char* getHint(char* secret, char* guess) {
char hashs[10] = { 0 };
char hashg[10] = { 0 };
int len = strlen(secret);
int bulls = 0, cows = 0;
int i;
for (i = 0; i < len; i++) {
if (secret[i] == guess[i]) {
bulls++;
}
else {
hashs[secret[i] - '0']++;
hashg[guess[i] - '0']++;
}
}
for (i = 0; i < 10; i++) {
cows += min(hashs[i], hashg[i]);
}
char *hint = (char *)malloc(5);
sprintf(hint, "%dA%dB", bulls, cows);
hint[5] = '\0';
return hint;
}
int main() {
assert(strcmp(getHint("1807", "7810"), "1A3B") == 0);
assert(strcmp(getHint("1234", "0111"), "0A1B") == 0);
assert(strcmp(getHint("1122", "2211"), "0A4B") == 0);
printf("all tests passed!\n");
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
const getHint = function(secret, guess) {
let bulls = 0
let cows = 0
const h = {}
for(let i = 0, len = secret.length; i < len; i++) {
if(secret[i] === guess[i]) {
bulls++
} else {
if(!h.hasOwnProperty(secret[i])) h[secret[i]] = 0
h[secret[i]]++
}
}
for(let i = 0, len = secret.length; i < len; i++) {
if(secret[i] !== guess[i]) {
if(h.hasOwnProperty(guess[i]> && h[guess[i]] > 0) {
cows++
h[guess[i]]--
}
}
}
return `${bulls}A${cows}B`
};
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Python Programming
Code -
Python Programming
class Solution:
def getHint(self, secret, guess):
s, g, a, b = collections.defaultdict(int), collections.defaultdict(int), 0, 0
for i in range(len(secret)):
if secret[i] == guess[i]: a += 1; continue
if s[guess[i]] > 0: b, s[guess[i]] = b + 1, s[guess[i]] - 1
else: g[guess[i]] += 1
if g[secret[i]] > 0: b, g[secret[i]] = b + 1, g[secret[i]] - 1
else: s[secret[i]] += 1
return "%dA%dB" % (a, b)
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with C# Programming
Code -
C# Programming
namespace LeetCode
{
public class _0299_BullsAndCows
{
public string GetHint(string secret, string guess)
{
var counts = new int[10];
foreach (var ch in secret)
counts[ch - '0']++;
int a = 0, b = 0;
for (int i = 0; i < guess.Length; i++)
{
if (guess[i] == secret[i])
{
a++;
if (counts[guess[i] - '0'] > 0)
counts[guess[i] - '0']--;
else
b--;
continue;
}
if (counts[guess[i] - '0'] > 0)
{
b++;
counts[guess[i] - '0']--;
}
}
return $"{a}A{b}B";
}
}
}
Copy The Code &
Try With Live Editor
Input
Output