<LeetCode><Easy>299 Bulls and Cows

原创 2015年11月17日 23:25:44

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)






LeetCode299——Bulls and Cows我的解法

You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...

Leetcode 299. Bulls and Cows (Python)

终于瞌睡了题目大意给定两个字符串 位置相同,字符相同,A加一 位置不同,存在相同字符,B加一 算法就是用hash表做代码class Solution(object): def getHint(...

Leetcode 299. Bulls and Cows (Easy) (cpp)

Leetcode 299. Bulls and Cows (Easy) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年07月12日 12:41
  • 223

299. Bulls and Cows [easy] (Python)

题目链接https://leetcode.com/problems/bulls-and-cows/题目原文 You are playing the following Bulls and Cows...

LeetCode--299. Bulls and Cows

Problem:You are playing the following Bulls and Cows game with your friend: You write down a number ...

LeetCode 299 Bulls and Cows

题目描述You are playing the following Bulls and Cows game with your friend: You write down a number and ...

LeetCode笔记:299. Bulls and Cows

又一个猜数字的游戏

leetcode299. Bulls and Cows

299. Bulls and Cows 问题描述: You are playing the following Bulls and Cows game with your friend...

Leetcode:299. Bulls and Cows(JAVA)

【问题描述】 You are playing the following Bulls and Cows game with your friend: You write down a num...

Leetcode_299_Bulls and Cows

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/50768550 You are playing ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:<LeetCode><Easy>299 Bulls and Cows
举报原因:
原因补充:

(最多只允许输入30个字)