[Python] One pass, O(1) space...All right it's O(#symbols) space


  • 0
    M
    '''
    "Bulls" is a special kind of "Cows"...
    '''
    class Solution(object):
        def getHint(self, secret, guess):
            """
            :type secret: str
            :type guess: str
            :rtype: str
            """
            n = len(secret)
            s, g = [0] * 10, [0] * 10
            bulls, cows = 0, 0
            for i in xrange(n):
                if secret[i] == guess[i]:
                    bulls += 1
                s[int(secret[i])] += 1
                g[int(guess[i])] += 1
            cows = sum([min(s[i], g[i]) for i in xrange(10)]) - bulls
            return str(bulls) + "A" + str(cows) + "B"

Log in to reply
 

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