# <LeetCode><Easy>299 Bulls and Cows

### Bulls and Cows

Total Accepted: 7348 Total Submissions: 29997 Difficulty: Easy

You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number.

For example:

Secret number:  "1807"
Friend's guess: "7810"

Hint: 1 bull and 3 cows. (The bull is 8, the cows are 01 and 7.)

Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return "1A3B".

Please note that both secret number and friend's guess may contain duplicate digits, for example:

Secret number:  "1123"
Friend's guess: "0111"

In this case, the 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return "1A1B".

You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.

Credits:
Special thanks to @jeantimex for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

#python27 108ms

class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
length=len(secret)
countA=0
secret=[int(s) for s in secret]
guess=[int(g) for g in guess]
for i in range(length):
if secret[i]==guess[i]:
countA+=1
secret[i]='x'   #block the number
guess[i]='x'
countB=0
for i in range(10):
countB+=min(secret.count(i),guess.count(i))
return "%sA%sB"%(countA,countB)

print(Solution().getHint("18070","78100"))

#Python2 84ms

class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
countA,i,numList=0,-1,[[0,0] for i in range(10)]
while 1:
i+=1
try:
if secret[i]==guess[i]:countA+=1
else:
numList[int(secret[i])][0]+=1
numList[int(guess[i])][1]+=1
except:
break
return "%sA%sB"%(countA,sum(min(i,v) for i,v in numList))

#Python27   100ms

class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
countA,i,numDict=0,-1,{i:[0,0] for i in range(10)}
while 1:
i+=1
try:
if secret[i]==guess[i]:countA+=1
else:
numDict[int(secret[i])][0]+=1
numDict[int(guess[i])][1]+=1
except:
break
return "%sA%sB"%(countA,sum(min(numDict[key]) for key in numDict))

#Python27   72ms

class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
countA=[]
numDict1={str(i):0 for i in range(10)}
numDict2={str(i):0 for i in range(10)}
def mapFunction(x,y):
if x==y:
countA.append(1)
else:
numDict1[x]+=1
numDict2[y]+=1
map(mapFunction,secret,guess)
return "%sA%sB"%(len(countA),sum(min(numDict1[key],numDict2[key]) for key in [str(i) for i in range(10)]))


#Python27  72ms

class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
countA=[]
numDict={str(i):[0,0] for i in range(10)}
def mapFunction(x,y):
if x==y:
countA.append(1)
else:
numDict[x][0]+=1
numDict[y][1]+=1
map(mapFunction,secret,guess)
return "%sA%sB"%(len(countA),sum(min(numDict[key]) for key in [str(i) for i in range(10)]))

#Python27 96ms

class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
countA=[]
numList1,numList2=[0]*10,[0]*10
def mapFunction(x,y):
if x==y:
countA.append(1)
else:
numList1[int(x)]+=1
numList2[int(y)]+=1
map(mapFunction,secret,guess)
return "%sA%sB"%(len(countA),sum(min(numList1[i],numList2[i]) for i in range(10)))

#Python27  124ms

class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
i,countA=0,0
secret,guess=list(secret),list(guess)
while 1:
try:
if secret[i]==guess[i]:
countA+=1
secret.pop(i)
guess.pop(i)
continue
else:
pass
except:    #out of index
break
i+=1
countB=sum([min(secret.count(str(i)),guess.count(str(i))) for i in range(10)])
return "%sA%sB"%(countA,countB)

#Python27 72ms

class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
numDict1={str(i):[] for i in range(10)}
numDict2={str(i):[] for i in range(10)}
for i,s in enumerate(secret):
numDict1[s].append(i)
for i,g in enumerate(guess):
numDict2[g].append(i)
countA=0
countB=0
for key in [str(i) for i in range(10)]:
indexList1,indexList2=set(numDict1[key]),set(numDict2[key])
intersection=len(indexList1&indexList2)   #we use set
countA+=intersection
countB+=min(len(indexList1)-intersection,len(indexList2)-intersection)
return "%sA%sB"%(countA,countB)

• 本文已收录于以下专栏：

## 【Leetcode】299-bulls-and-cows【Java实现】【hash table】

• ALiTuTu
• 2015年11月05日 17:48
• 1056

## LeetCode299——Bulls and Cows我的解法

You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
• booirror
• 2015年11月28日 14:53
• 2228

## LeetCode || 299. Bulls and Cows

299. Bulls and Cows You are playing the following Bulls and Cows game with your friend: You write ...
• xsloop
• 2016年02月24日 20:13
• 595

## Leetcode 299. Bulls and Cows (Python)

• wds2006sdo
• 2016年07月19日 03:52
• 3020

## leetcode 299 : Bulls and Cows

1、原题如下： You are playing the following Bulls and Cows game with your friend: You write a 4-digit sec...
• l3368bcttqnqn
• 2015年11月02日 18:21
• 147

## [leetcode 299] Bulls and Cows

Quesiton: You are playing the following Bulls and Cows game with your friend: You write down a numb...
• Xiaohei00000
• 2015年11月30日 22:46
• 190

## LeetCode 299:Bulls and Cows

You are playing the following Bulls and Cows game with your friend: You write down a number and as...
• 2015年12月20日 23:14
• 169

## leetcode 299 Bulls and Cows

You are playing the following Bulls and Cows game with your friend: You write down a number and as...
• suichen1
• 2015年12月14日 23:40
• 206

## LeetCode 299 Bulls and Cows

• Yano_nankai
• 2015年12月14日 15:36
• 605

## [LeetCode 299] Bulls and Cows

You are playing the following Bulls and Cows game with your friend: You write down a number and ask...
• sbitswc
• 2015年11月19日 16:19
• 1115

举报原因： 您举报文章：299 Bulls and Cows 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)