299. Bulls and Cows

https://leetcode.com/problems/bulls-and-cows/

solution

class Solution:
    def getHint(self, secret: str, guess: str) -> str:
        a = {}
        b = {}
        for i in secret:           
            if i in a:
                a[i] += 1
            else:
                a[i] = 1
        for i in guess:            
            if i in b:
                b[i] += 1
            else:
                b[i] = 1
        
        bulls = 0
        for i in range(len(secret)): # Counting bulls
            if secret[i] == guess[i]:
                bulls += 1
                a[secret[i]] -= 1
                b[secret[i]] -= 1
        cows = 0
        for i in a: # Counting cows
            if i in b:
                cows += min(a[i], b[i])
        res = str(bulls) + "A" + str(cows) + "B"
        return res

时间复杂度:O(n) 空间复杂度:O(n)

Last updated