KatInterview

Letter-Grid Feedback for a Hidden Word Guess

StringsAuto-graded · 21 test cases
MediumAuto-gradedStringsstringshash-mapcounting

Last updated

Score a guess against a hidden word with exact, misplaced, and absent marks, handling repeated letters correctly and rejecting invalid input.

Letter-Grid Feedback for a Hidden Word Guess

You are building the scoring step of a word-guessing game. Given a hidden word target and a player's attempt of the same length, produce a list of integer marks, one per position, describing how well each letter of the attempt fits.

Marks

For each index i:

  • 2 (exact): attempt[i] == target[i].
  • 1 (misplaced): the letter is not an exact match at i, but the target still has an unused copy of that letter at some other position (see the rules below).
  • 0 (absent): neither of the above. The letter does not appear in the target, or every relevant copy has already been used.

How copies are used up

Each letter of the target can justify at most one non-zero mark in the attempt:

  1. First, every exact match (mark 2) uses up the target letter at that same position.
  2. Every target letter at a position that was not an exact match forms a pool of available copies, counted per letter.
  3. Then scan the attempt from left to right over the positions that are not exact matches. If the pool still holds a copy of attempt[i], give that position mark 1 and remove one copy from the pool. Otherwise give mark 0.

So when an attempt has more leftover copies of a letter than the target can supply, the leftmost ones receive mark 1 and the rest receive 0.

Input validation

Raise a ValueError if any of the following hold:

  • target or attempt is not a string (this includes None),
  • either string is empty,
  • the two strings have different lengths,
  • either string contains a character outside 'a' to 'z' (uppercase letters, digits, spaces, and non-ASCII characters are all invalid).

Validation must happen before any scoring.

Task

Implement the function that returns the list of marks.

  • Input: target: str, attempt: str
  • Output: a list of integers, each in {0, 1, 2}, with the same length as the inputs.

Constraints

  • Valid inputs consist only of lowercase English letters a-z.
  • Valid inputs are non-empty and both have the same length n.
  • Invalid inputs must raise ValueError.
  • The solution should run in O(n) time.

Examples

Example 1

Input: score_attempt('hello', 'abcde')

Output: [0, 0, 0, 0, 1]

Example 2

Input: score_attempt('hello', 'eabcd')

Output: [1, 0, 0, 0, 0]

Example 3

Input: score_attempt('aabb', 'bbba')

Output: [1, 0, 2, 1]

Hints

Hint 1

Think about what goes wrong if you mark a letter as misplaced just because it appears somewhere in the target. A single target letter should only justify one mark.

Hint 2

Do two passes. First lock in all exact matches, and count the target letters at the remaining positions. Only then decide which other positions get a misplaced mark.

Hint 3

In the second pass, walk left to right over non-exact positions, and whenever the count for that attempt letter is positive, give mark 1 and decrement the count. Do all validation up front.