Python O(n) Solution


  • 0
    S
    class Solution(object):
        def getHint(self, secret, guess):
            secret_numbers = [0] * 10
            guess_numbers = [0] * 10
            cows = 0
            bulls = 0
            for i in range(len(secret)):
                if secret[i] == guess[i]:
                    bulls += 1
                else:
                    if guess_numbers[int(secret[i])] > 0:
                        guess_numbers[int(secret[i])] -= 1
                        cows += 1
                    else:
                        secret_numbers[int(secret[i])] += 1
                    if secret_numbers[int(guess[i])] > 0:
                        secret_numbers[int(guess[i])] -= 1
                        cows += 1
                    else:
                        guess_numbers[int(guess[i])] += 1
            return "{}A{}B".format(bulls, cows)

Log in to reply
 

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