320. Bulls and Cows

Medium
Frequency Reconciliation
Hashing

Problem

You are given two equal-length digit strings secret and guess. A bull is a digit that matches in both value and position. A cow is a non-bull digit from guess that exists elsewhere in secret. Return the hint in the form xAyB, where x is the number of bulls and y is the number of cows.

Examples

Example 1

Input: "1807", "7810"
Output: "1A3B"

Example 2

Input: "1123", "0111"
Output: "1A1B"

Example 3

Input: "1", "0"
Output: "0A0B"
Constraints

1 <= secret.length == guess.length <= 1000

secret and guess consist only of digits.

Hints

?? Count exact-position matches as bulls immediately.

?? Only unmatched digits can become cows.

?? Frequency counts for the unmatched digits avoid double counting.

Expected Complexity
Time: O(n)
Space: O(1)
Follow-up

Can you count bulls and cows in one pass using a fixed array of size 10?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
"1807", "7810"
Expected
"1A3B"