Concise python code


  • 0

    consider it as a producer-consumer problem.

    class Solution(object):
        def getHint(self, secret, guess):
            A, B, record = 0, 0, {}
            for i, s in enumerate(secret):
                g = guess[i]
                if s == g:
                    A += 1
                else:
                    numS, numG = record.get(s, 0), record.get(g, 0)
                    record[s], record[g] = numS + 1, numG - 1
                    B += (numS < 0) + (numG > 0)
    
            return '{}A{}B'.format(A, B)

Log in to reply
 

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