Similar Problems

Similar Problems not available

Bulls And Cows - Leetcode Solution

Companies:

LeetCode:  Bulls And Cows Leetcode Solution

Difficulty: Medium

Topics: string hash-table  

Bulls and Cows is a classic game where one player thinks of a secret 4-digit number and the other player has to guess the number. After each guess, the first player gives clues in the form of "bulls" and "cows". "Bulls" represent the number of digits in the guess that match the digits in the correct position in the secret number, while "cows" represent the number of digits in the guess that match a digit in the secret number, but are in the wrong position.

The problem on LeetCode is to implement a function that takes two strings as input - the secret number and a guess - and returns the number of bulls and cows.

For example, if the secret number is "1234" and the guess is "5678" the output should be "0A0B", as there are no digits that match either in position or value. If the secret number is "1234" and the guess is "9381" the output should be "0A2B", as there are two digits (3 and 1) that match a digit in the secret number but are not in the correct position.

Here is one possible solution to the problem:

def getHint(secret: str, guess: str) -> str:
    bulls = 0
    cows = 0
    secret_count = {}
    guess_count = {}
    for i in range(len(secret)):
        if secret[i] == guess[i]:
            bulls += 1
        else:
            if secret[i] in guess_count:
                cows += 1
                guess_count[secret[i]] -= 1
                if guess_count[secret[i]] == 0:
                    del guess_count[secret[i]]
            else:
                if secret[i] in secret_count:
                    secret_count[secret[i]] += 1
                else:
                    secret_count[secret[i]] = 1
            if guess[i] in secret_count:
                cows += 1
                secret_count[guess[i]] -= 1
                if secret_count[guess[i]] == 0:
                    del secret_count[guess[i]]
            else:
                if guess[i] in guess_count:
                    guess_count[guess[i]] += 1
                else:
                    guess_count[guess[i]] = 1
    return '{}A{}B'.format(bulls, cows)

The function iterates over the digits in both strings, counting the number of bulls and cows as it goes. If a digit appears in both the secret and guess strings and is in the same position in both strings, it is a bull and the count of bulls is incremented. Otherwise, the digit is a cow.

To count cows, the function keeps track of a count of the number of times each digit appears in the secret and guess strings separately. If a digit appears in the guess string but not in the same position as in the secret string, the function checks the count of that digit in the secret string. If the count is greater than zero, the function counts the digit as a cow and decrements the count of that digit in the secret string. If the count of that digit in the secret string is now zero, the function removes that digit from the dictionary of counts of digits in the secret string.

The same process is repeated for each digit in the secret and guess strings, and at the end the function returns a string in the format "xAyB" where x is the number of bulls and y is the number of cows.

Bulls And Cows Solution Code

1