Skip to main content

299. Bulls and Cows

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

Python

from collections import Counter


class Solution:
def getHint(self, secret: str, guess: str) -> str:
counts = Counter(secret)

bulls, cows = 0, 0
for i, num in enumerate(guess):
if num == secret[i]:
bulls += 1
cows -= 1 if counts[num] <= 0 else 0
else:
cows += 1 if counts[num] > 0 else 0
counts[num] -= 1
return "{}A{}B".format(bulls, cows)