# <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)

2015-10-31 10:48:27

#### Leetcode_299_Bulls and Cows

2016-02-29 21:49:45

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

2015-11-28 14:53:27

#### LeetCode || 299. Bulls and Cows

2016-02-24 20:13:55

#### LeetCode -- Bulls and Cows

2015-11-16 18:15:24

#### leetcode -- Bulls and Cows -- 难理解题意，要看

2015-12-08 23:04:58

#### Leetcode 299. Bulls and Cows (Python)

2016-07-19 03:52:50

#### leetcode题解-299. Bulls and Cows

2017-02-24 21:27:13

#### Bulls and Cows问题及解法

2017-11-22 09:29:18

#### [299] Bulls and Cows

2016-07-26 09:19:53