Skip to content

0299. 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.

Example 3:

Input: secret = "1", guess = "0"
Output: "0A0B"

Example 4:

Input: secret = "1", guess = "1"
Output: "1A0B"

Constraints:

  • 1 <= secret.length, guess.length <= 1000
  • secret.length == guess.length
  • secret and guess consist of digits only.

Analysis

We need to calculate the number of "bulls" and "cows" given two strings. To calculate "bulls" is simple: just check each index of the two strings, and if the elements they are point to each are equal, we increment our "bulls" count.

Calculating "cows" is a little harder. If a character from guess appeared in secret (no matter before current index or after), we increment our "cows" count. Besides, if a guess[i] already matched with a secret[j], we cannot reuse secret[j] again. In order to solve this problem, we can use a map to record how many avalible secret[j] we can use, if there are secret[k] == secret[j] and k != j.

After we built this map, we can run a check on each character from guess string, and keep updating the value of the map until it reaches 0.

  • Time: O(n)
  • Space: O(n) when all characters in secret string are distinct.

Code

class Solution {
public:
    string getHint(string secret, string guess) {
        int cows = 0, bulls = 0;
        int n = secret.size();
        unordered_map<int, int> mp;
        for (int i = 0; i < n; ++i) {
            if (secret[i] == guess[i])
                bulls ++;
            else 
                mp[secret[i]]++;            
        }
        for (int i = 0; i < n; ++i) {
            if (secret[i] != guess[i] && mp[guess[i]]-- > 0) 
                cows ++;
        }
        return to_string(bulls) + "A" + to_string(cows) + "B";
    }
};

Last update: April 1, 2022