Ctrl K

A Hint for a Digit Guess

Medium
One player picks a secret digit string code, and the other tries to guess it by naming a string attempt of the same length. The guesser gets a hint back: - how many digits are right and in the right position; call them **exact**; - how many more digits of the attempt appear in the code but in other positions. Precisely, it is the largest number of the remaining (non-exact) attempt digits that could be rearranged to become exact. Each digit of the code counts at most once. Return the hint as the string "xAyB", where x is the number of exact digits and y the number of digits found in the wrong place. Both code and attempt may contain repeated digits.
Example 1
code='2468', attempt='8462' → '2A2B'

4 and 6 are in the right places. 8 and 2 are in the code but elsewhere, giving 2A2B.

Example 2
code='5500', attempt='0055' → '0A4B'

No digit is in place, yet all four can be rearranged to match: 0A4B.

Example 3
code='112', attempt='211' → '1A2B'

The middle 1 is in place. Of the remaining "12" and "21", both digits occur in the code but in other places: 1A2B.

Constraints
  • 1 ≤ len(code) ≤ 10⁵
  • len(attempt) = len(code)
  • code and attempt consist of digits only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCharacters Grouped by Count MediumMost Searched Words MediumNo Twin Letters Side by Side MediumDomain Visit Rollup MediumCount the Tile Sequences Medium
loading Python…
Input
code='2468', attempt='8462'
Expected
'2A2B'

Run checks the first 3 tests in your browser. Submit checks all 118 tests on the server, hidden ones included.