LeetCode 299 Bulls and Cows

LeetCode 299 Bulls and Cows Problem

Download Code
class Solution(object):
    def getHint(self, secret, guess):
        :type secret: str
        :type guess: str
        :rtype: str
        check = {}
        ls = len(secret)
        bull, cow = 0, 0
        different = []
        for i in range(ls):
            if guess[i] == secret[i]:
                bull += 1
                # store possible index and count for cow
                    check[secret[i]] += 1
                except KeyError:
                    check[secret[i]] = 1
        for i in different:
                if check[guess[i]] > 0:
                    cow += 1
                    check[guess[i]] -= 1
        return "%dA%dB" % (bull, cow)

if __name__ == "__main__":
    s = Solution()
    print s.getHint("1122", "1222")
Download Bulls and Cows.py

List of all Bulls and Cows problems

Leetcode 299 Bulls and Cows problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.