<LeetCode><Easy>299 Bulls and Cows

Bulls and Cows

My Submissions
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)






阅读更多
文章标签: python leetcode
个人分类: python Easy LeetCode
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭