/299. Bulls and Cows

299. Bulls and Cows

Medium
Hash Table52.3% acceptance

Given two strings of equal length, secret_code and guess_code, both consisting of digits, compute a hint string in the format "xAyB" where x is the number of positions where secret_code and guess_code have the same digit (bulls), and y is the number of digits in guess_code that exist in secret_code but are not in the same position (cows). Each digit in secret_code and guess_code can only be counted once for bulls or cows. Return the hint string.

Example 1

Input: secret_code=1234, guess_code=4321

Output: 0A4B

Explanation: No bulls, all digits are cows.

Example 2

Input: secret_code=5555, guess_code=5566

Output: 2A0B

Explanation: Two bulls (positions 0 and 1), no cows.

Example 3

Input: secret_code=9876, guess_code=9876

Output: 4A0B

Explanation: All bulls, no cows.

Constraints

  • 1 <= len(secret_code) <= 1000
  • len(secret_code) == len(guess_code)
  • secret_code and guess_code consist of digits (0-9) only
Python (current runtime)

Case 1

Input: secret_code='2468', guess_code='8642'

Expected: '0A4B'

Case 2

Input: secret_code='0000', guess_code='1000'

Expected: '3A0B'

Case 3

Input: secret_code='1230', guess_code='3210'

Expected: '2A2B'

Case 4

Input: secret_code='1112', guess_code='2111'

Expected: '1A2B'

Case 5

Input: secret_code='7890', guess_code='0987'

Expected: '0A4B'