Bulls and Cows
You hold a string of digits secret and a friend guesses a string guess of the same length.
- A bull is a position where the guess has the same digit as the secret.
- A cow is a digit of the guess that is in the secret but at a different position, with each secret digit used at most once and bulls not counted again.
Return the hint as "xAyB", where x is the number of bulls and y the number of cows.
Example 1
secret "1807"
guess "7810"
returns "1A3B"
One bull (the 8), three cows (1, 0, 7): 1A3B.
Example 2
secret "1123"
guess "0111"
returns "1A1B"
One bull, one cow: 1A1B.
Constraints
1 <= len(secret) <= 20len(guess) == len(secret)- Both strings have only digits
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Hash maps problems
- Contiguous ArrayMedium
- Group AnagramsMedium
- Longest Consecutive SequenceMedium
- Longest Zero-Sum StretchMedium
- Rows That Match ColumnsMedium
- Shift FamiliesMedium
- Stretches With K Odd ValuesMedium
- Subarray Sum Equals KMedium