KatInterview

Letter Tile Scorer for a Word-Guessing Game

StringsAuto-graded · 21 test cases
MediumAuto-gradedStringsstringsarrayshashing

Last updated

Tests whether you can implement a precise per-letter color-scoring rule — including count-limited yellow matching — across a list of guesses against a fixed target word.

Letter Tile Scorer for a Word-Guessing Game

You are implementing the core scoring engine for a word-guessing game. Given a secret target word and a list of guesses, produce a color label for every letter in every guess.

Coloring Rules

For each guess, assign one of three labels to each letter position:

  • "green" — the letter matches the target at this exact position.
  • "yellow" — the letter exists somewhere in the target, but not at this position, and an unmatched copy of it still remains in the target.
  • "red" — none of the above applies: the letter is absent from the target, or all copies of it in the target have already been claimed.

Matching Priority

Green matches are resolved first. Only after all green positions are settled are the remaining (non-green) positions scanned left to right for yellow matches. Each copy of a letter in the target can be claimed by at most one position, so the number of yellow labels for a given letter never exceeds the number of unmatched copies left after greens are removed.

Case and Length

  • Matching is case-insensitive; normalize to lowercase before comparing.
  • Guesses may be any length. Every character in a guess produces exactly one output label.

Input

ParameterTypeDescription
targetstrThe secret word to guess against
guessesList[str]Ordered list of guesses to score

Output

Return a List[List[str]]: one inner list per guess, each containing the color labels ("green", "yellow", or "red") in left-to-right order.

Constraints

  • 1 ≤ len(target) ≤ 50
  • 1 ≤ len(guesses) ≤ 100
  • 1 ≤ len(guess) ≤ 50 for each guess in guesses
  • target and every guess contain only ASCII letters (a-z, A-Z)
  • The same guess may appear more than once in guesses
  • A guess that is longer or shorter than target is still scored; positions beyond the length of target are always "red"

Examples

Example 1

Input: score_guesses('apple', ['apple', 'ample', 'grape'])

Output: [['green', 'green', 'green', 'green', 'green'], ['green', 'red', 'green', 'green', 'green'], ['red', 'red', 'yellow', 'yellow', 'green']]

Example 2

Input: score_guesses('crate', ['trace', 'crane', 'xxxxx'])

Output: [['yellow', 'green', 'green', 'yellow', 'green'], ['green', 'green', 'green', 'red', 'green'], ['red', 'red', 'red', 'red', 'red']]

Example 3

Input: score_guesses('abbey', ['aaaaa', 'babbb', 'ABBey'])

Output: [['green', 'red', 'red', 'red', 'red'], ['yellow', 'yellow', 'green', 'red', 'red'], ['green', 'green', 'green', 'green', 'green']]

Hints

Hint 1

Start by counting the frequency of each letter in the target. Green matches reduce that count before any yellow check happens — process all positions for green first, then loop again for yellow.

Hint 2

Because greens are resolved in a separate pass, you need to decrement the remaining-count for a letter as soon as you assign it green, so that pass two cannot also award yellow for the same target copy.

Hint 3

Walk the guess left to right in the second pass and greedily assign yellow whenever the remaining count for that letter is still above zero, decrementing as you go. This naturally limits yellows to the number of unmatched target copies.