Medium
Quiz
#299 Bulls and Cows
APPROACH
In the Bulls and Cows guessing game you write down a secret number. When your friend guesses, you provide a hint with:
- The number of "bulls": digits that are correct in both value and position.
- The number of "cows": non-bull digits in the guess that appear in the secret but at the wrong position — specifically, the non-bull guess digits that could be rearranged to become bulls.
Given secret and guess, return the hint as "xAyB" where x is the bull count and y is the cow count. Both strings may contain duplicate digits.
Example 1:
Input: secret = "1807", guess = "7810"
Output: "1A3B"
Explanation: Bulls are connected with a '|' and cows are underlined:
"1807"
|
"7810"
Example 2:
Input: secret = "1123", guess = "0111"
Output: "1A1B"
Explanation: Bulls are connected with a '|' and cows are underlined:
"1123" "1123"
| or |
"0111" "0111"
Note that only one of the two unmatched 1s is counted as a cow since the non-bull digits can only be rearranged to allow one 1 to be a bull.
1 of 4
1:00
What is the optimal approach for this problem?