PYTHON dict solution


  • 4
    class Solution(object):
        def getHint(self, secret, guess):
            """
            :type secret: str
            :type guess: str
            :rtype: str
                    """
            d = {}
            bull, cow = 0,0
    
            for index,s in enumerate(secret):
                if guess[index] == s:
                    bull += 1
                else:
                    d[s] = d.get(s,0) + 1
    
            for index,s in enumerate(secret):
                if (guess[index] != s) & (d.get(guess[index],0) != 0):
            	    cow += 1
            	    d[guess[index]] -= 1
            	    
            return str(bull) + "A" + str(cow) + "B"

Log in to reply
 

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