Simple Python Code


  • 1
    S

    I think there are 2 steps.

    Step1: find the right digits.

    Step2: determine whether those digits are in the right place or not.

    class Solution(object):
    def getHint(self, s, g):
        """
        :type secret: str
        :type guess: str
        :rtype: str
        """
        counts = [0]*10
        cows = bulls = 0
        for i in range (0, len(s)):
            if s[i]==g[i]: bulls +=1
            else:
                index = int(s[i])
                counts[index] +=1
                if counts[index] <=0: cows+=1
                index = int(g[i])
                counts[index] -=1
                if counts[index] >=0: cows+=1
        return str(bulls)+"A"+str(cows)+"B"

  • -2

    def get_BullCowsCount(s,g):
    Cows=0
    Bulls=0
    secret = s
    hint = g
    for i in range (0,len(secret),1):
    if s[i]==g[i]:
    Bulls=Bulls+1
    else :
    value = s[i]
    for j in range(0,len(hint)
    ,1):
    if value==g[j]:
    Cows=Cows+1
    print "Cows"+ str(Cows) + "Bulls" + str(Bulls)

    get_BullCowsCount('1464','1536')


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.