You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number.

Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows. 

Please note that both secret number and friend's guess may contain duplicate digits.

Example 1:


Input: secret = "1807", guess = "7810"

Output: "1A3B"

Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.

Example 2:


Input: secret = "1123", guess = "0111"

Output: "1A1B"

Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow.

Note: You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.

Difficulty: MEDIUM_HARD
Tags: String algorithms

Data structures

Mathematics
Skill types: Data structures

Starter code:
class Solution:
    def getHint(self, secret: str, guess: str) -> str:
        

Solutions:
class Solution:

	def getHint(self, secret, guess):
		s_count = collections.defaultdict(int)
		g_count = collections.defaultdict(int)
		bull_cnt = 0
		for (s, g) in zip(secret, guess):
			if s == g:
				bull_cnt += 1
			else:
				s_count[s] += 1
				g_count[g] += 1
		cow_cnt = sum((min(s_count[x], g_count[x]) for x in g_count if x in s_count))
		return '{}A{}B'.format(bull_cnt, cow_cnt)


class Solution:

	def getHint(self, secret, guess):
		secretDict = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
		friendDict = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
		bulls = 0
		cows = 0
		for (s, g) in zip(secret, guess):
			if s == g:
				bulls += 1
			else:
				secretDict[int(s)] += 1
				friendDict[int(g)] += 1
		for i in range(10):
			cows += min(secretDict[i], friendDict[i])
		return '{}A{}B'.format(bulls, cows)


class Solution:

	def getHint(self, secret, guess):
		need = {}
		seen = {}
		ret_A = 0
		ret_B = 0
		for i in range(len(secret)):
			if secret[i] == guess[i]:
				ret_A += 1
			else:
				if seen.get(secret[i], 0) > 0:
					ret_B += 1
					seen[secret[i]] -= 1
				else:
					need[secret[i]] = need.get(secret[i], 0) + 1
				if need.get(guess[i], 0) > 0:
					ret_B += 1
					need[guess[i]] -= 1
				else:
					seen[guess[i]] = seen.get(guess[i], 0) + 1
		return str(ret_A) + 'A' + str(ret_B) + 'B'


class Solution:

	def getHint(self, secret, guess):
		(dict, A, B) = ({}, 0, 0)
		dicthash = {}
		for i in secret:
			if i in dicthash:
				dicthash[i] += 1
			else:
				dicthash[i] = 1
		for i in guess:
			if i in dicthash:
				B += 1
				if dicthash[i] == 1:
					del dicthash[i]
				else:
					dicthash[i] -= 1
		for (i, char) in enumerate(list(secret)):
			if char == guess[i]:
				A += 1
				B -= 1
		return '{}A{}B'.format(A, B)


class Solution(object):

	def getHint(self, secret, guess):
		d = {}
		(bull, cow) = (0, 0)
		for (index, s) in enumerate(secret):
			if guess[index] == s:
				bull += 1
			else:
				d[s] = d.get(s, 0) + 1
		for (index, s) in enumerate(secret):
			if (guess[index] != s) & (d.get(guess[index], 0) != 0):
				cow += 1
				d[guess[index]] -= 1
		return str(bull) + 'A' + str(cow) + 'B'


class Solution:

	def getHint(self, secret, guess):
		bulls = cows = 0
		hist = [0] * 10
		for i in range(len(secret)):
			if secret[i] == guess[i]:
				bulls += 1
			else:
				hist[ord(secret[i]) - ord('0')] += 1
				if hist[ord(secret[i]) - ord('0')] - 1 < 0:
					cows += 1
				hist[ord(guess[i]) - ord('0')] -= 1
				if hist[ord(guess[i]) - ord('0')] + 1 > 0:
					cows += 1
		return str(bulls) + 'A' + str(cows) + 'B'


class Solution:

	def getHint(self, secret, guess):
		bull = 0
		cow = 0
		for i in range(len(secret)):
			if secret[i] == guess[i]:
				bull += 1
				secret = secret[:i] + 'a' + secret[i + 1:]
				guess = guess[:i] + 'b' + guess[i + 1:]
		secret = ''.join(sorted(secret))
		guess = ''.join(sorted(guess))
		i = 0
		j = 0
		while i < len(guess) and j < len(guess):
			if secret[i] == 'a':
				break
			if guess[j] == 'b':
				break
			if secret[i] == guess[j]:
				cow += 1
				i += 1
				j += 1
				continue
			if secret[i] < guess[j]:
				i += 1
				continue
			if secret[i] > guess[j]:
				j += 1
				continue
		print((secret, guess))
		return str(bull) + 'A' + str(cow) + 'B'


class Solution:

	def getHint(self, secret, guess):
		d1 = {}
		d2 = {}
		for (i, c) in enumerate(secret):
			if c not in d1:
				d1[c] = []
			d1[c].append(i)
		for (i, c) in enumerate(guess):
			if c not in d2:
				d2[c] = []
			d2[c].append(i)
		(bulls, cows) = (0, 0)
		for k in d2:
			if k not in d1:
				continue
			curBulls = curCows = 0
			posGuess = d2[k]
			posSecret = d1[k]
			for p in posGuess:
				if p in posSecret:
					curBulls += 1
			curCows = max(0, min(len(posGuess) - curBulls, len(posSecret) - curBulls))
			bulls += curBulls
			cows += curCows
		return str(bulls) + 'A' + str(cows) + 'B'


class Solution:

	def getHint(self, secret, guess):
		bulls = 0
		mismatch_chars = collections.defaultdict(int)
		for (secret_char, guess_char) in zip(secret, guess):
			if secret_char == guess_char:
				bulls += 1
			else:
				mismatch_chars[secret_char] += 1
		cows = 0
		for (secret_char, guess_char) in zip(secret, guess):
			if secret_char != guess_char and mismatch_chars[guess_char] > 0:
				mismatch_chars[guess_char] -= 1
				cows += 1
		return '%sA%sB' % (bulls, cows)