Easy Python List Solution

  • 0
    class Solution(object):
        def getHint(self, secret, guess):
            bulls = 0
            cows = 0
            cowcount = [0]*10
            for i in range(10):
                s = str(i)
                cowcount[i] = min(secret.count(s),guess.count(s))
            for i in range(len(secret)):
                if secret[i] == guess[i]:
                    bulls += 1
                    j = int(secret[i])
                    cowcount[j] -= 1
            for s in cowcount:
                cows += int(s)
            return str(bulls)+"A"+str(cows)+"B"

    Check for cows (the number of cows for a digit can't ever exceed secret, hence the min). Then check for bulls -- every bull is one less cow. Then calculate the total number of cows and return.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.