In this Leetcode Bulls and Cows problem solution 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.

Leetcode Bulls and Cows problem solution


Problem solution in Python.

class Solution:

    def getHint(self, secret: str, guess: str) -> str:

        A = 0
        B = 0
        s_list = ['']*len(secret)
        g_list = ['']*len(guess)


        for i in range(len(secret)):
            s_list[i] = secret[i]
            g_list[i] = guess[i]

        # Check bulls
        for i in range(len(secret)):
            if secret[i] == guess[i]: 
                A += 1
                s_list.remove(secret[i])
                g_list.remove(guess[i])

        # Check cows
        for i in range(len(g_list)):
            if g_list[i] in s_list:
                B += 1
                s_list.remove(g_list[i])


        str_ans = str(A) + "A" + str(B) + "B"

        return str_ans



Problem solution in Java.

public String getHint(String secret, String guess) {

    if(secret.length() == 0){return "0A0B";}
    
    int bull = 0;
    int cow = 0;
    int [] result = new int [10];
    
    for(int i = 0;i<secret.length();i++)
    {
        int x = secret.charAt(i) - 48;
        int y = guess.charAt(i) - 48;
        
        if(x == y)
        {
            bull++;
        }
        else
        {
            if(result[x] < 0){cow++;}
            result[x]++;
            
            if(result[y] > 0){cow++;}
            result[y]--;
        }
    }
    
    return bull+"A"+cow+"B";
    
}


Problem solution in C++.

class Solution {
public:
    string getHint(string secret, string guess) {
        int A = 0, B = 0, i = 0, len = 0;
        int digitS[10] = {0}, digitG[10] = {0};
        string ret = "";
        
        len = secret.length();
        for (i = 0; i < len; ++i)
        {
            if (secret[i] == guess[i])
                ++A;
            else
            {
                ++digitS[secret[i] - '0'];
                ++digitG[guess[i] - '0'];
            } 
        }
        for (i = 0; i < 10; ++i)
        {
            if (digitS[i] == digitG[i])
                B += digitS[i];
            else if (digitS[i] > 0 && digitG[i] > 0)
                B += (digitS[i] > digitG[i] ? digitG[i] : digitS[i]);
        }
        ret = to_string(A)+"A"+to_string(B)+"B";
        
        return ret;
    }
};


Problem solution in C.

char * getHint(char * secret, char * guess){
    int count[10] = {0};
    int i = 0, len = strlen(secret);
    int bulls = 0, cows = 0;
    char *ret = NULL;
    unsigned int bulldigits = 1, cowdigits = 1, outlen = 0;

    for (i = 0; i < len; i++) {
        int digit = secret[i] - '0';
        count[digit]++;
    }
    for (i = 0; i < len; i++) {
        int digit = guess[i] - '0';
        if (guess[i] == secret[i]) {
            bulls++;
            count[digit]--;
        } 
    }
    for (i = 0; i < len; i++) {
        int digit = guess[i] - '0';
        if (guess[i] != secret[i]) {
            if (count[digit] > 0) {
                cows++;
                count[digit]--;
            }
        }
    }

    if (bulls != 0) {
        bulldigits = (unsigned int) ((log10((double) bulls)) + 1);
    }
    
    if (cows != 0) {
        cowdigits = (unsigned int) ((log10((double) cows)) + 1);
    }
    
    outlen = bulldigits + cowdigits + 3;
    ret = (char *) malloc (outlen * sizeof(char));
    
    snprintf(ret, outlen, "%dA%dB", bulls, cows);
    
    return ret;
}