Letter Tile Scorer for a Word-Guessing Game
StringsAuto-graded · 21 test casesLast 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
| Parameter | Type | Description |
|---|---|---|
target | str | The secret word to guess against |
guesses | List[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) ≤ 501 ≤ len(guesses) ≤ 1001 ≤ len(guess) ≤ 50for each guess inguessestargetand 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
targetis still scored; positions beyond the length oftargetare 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is that the two rules — green and yellow — must be evaluated in separate passes, because green matches have priority and consume target-letter budget that yellows would otherwise claim.
Step-by-step
Build a frequency map. For each guess, count how many times each letter appears in the target. This map tracks how many "unclaimed" copies of each letter remain.
Pass 1 — greens. Walk every position in the guess. When the letter equals the target letter at the same index, mark it green and decrement its count in the frequency map. This ensures those copies cannot later trigger a yellow.
Pass 2 — yellows. Walk the positions again, skipping anything already green. If the frequency map still has a nonzero count for this letter, mark it yellow and decrement the count. Otherwise it stays red.
Why this is correct. Consider the classic edge case: target "abbey", guess "aaaaa". The target has one a. Pass 1 marks position 0 green and sets remaining['a'] = 0. Pass 2 finds no remaining a budget, so positions 1–4 all stay red. This matches the expected output ["green","red","red","red","red"].
Without the two-pass structure — if you tried to assign green and yellow in a single scan — an earlier yellow assignment might consume budget that a later green actually deserves, producing wrong counts.
Complexity
- Time: O(G × (T + N)) where G is the number of guesses, T is the length of the target, and N is the length of the current guess. Building the frequency map is O(T); each pass over the guess is O(N).
- Space: O(T) for the frequency map per guess, plus O(G × N) for the output.
Reference solution
from typing import List
def score_guesses(target: str, guesses: List[str]) -> List[List[str]]:
target = target.lower()
result = []
for guess in guesses:
guess = guess.lower()
colors = ["red"] * len(guess)
# Count available (unmatched) letters in the target
remaining = {}
for ch in target:
remaining[ch] = remaining.get(ch, 0) + 1
# Pass 1: assign greens and reduce the available count
for i, ch in enumerate(guess):
if i < len(target) and ch == target[i]:
colors[i] = "green"
remaining[ch] -= 1
# Pass 2: assign yellows from leftover target copies
for i, ch in enumerate(guess):
if colors[i] == "green":
continue
if remaining.get(ch, 0) > 0:
colors[i] = "yellow"
remaining[ch] -= 1
result.append(colors)
return result