题目链接:488. 祖玛游戏
广度优先搜索。
from collections import Counter
class Solution:
def getHint(self, secret: str, guess: str) -> str:
bulks, cows = 0, 0
alices, bobs = list(), list()
for a, b in zip(secret, guess):
if a == b:
bulks += 1
else:
alices.append(a)
bobs.append(b)
alices, bobs = Counter(alices), Counter(bobs)
for key in set(alices.keys()).intersection(bobs.keys()):
cows += min(alices.get(key), bobs.get(key))
return f'{bulks}A{cows}B'