Solution
-
cow
is related with the number of correct characters. Ifbull
has used up the corrected characters, a misplaced correct character is not considered ascow
. -
Count the number of
bull
. -
Count the sum of matched correct characters.
-
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)
PREVIOUSPartition Labels
NEXTEdit Distance