Bulls and Cows

 

299. Bulls and Cows

Solution

  1. cow is related with the number of correct characters. If bull has used up the corrected characters, a misplaced correct character is not considered as cow.

  2. Count the number of bull.

  3. Count the sum of matched correct characters.

  4. cow = matched - bull

    def getHint(self, secret, guess):
     """
     :type secret: str
     :type guess: str
     :rtype: str
     """
     bull, cow = 0, 0
     dic = collections.Counter(secret)
    
     for x, y in zip(secret, guess):
         if x == y:
             bull += 1
     cow = sum((dic & collections.Counter(guess)).values()) - bull
     return '{}A{}B'.format(bull, cow)