cswork / libraryOpen practice workspace

Coding questions · Ngrok

Generate Color Feedback for a Word Guess

Generate green, yellow, or red feedback for each character in a word guess based on positional equality and presence anywhere in the answer. Follow the stated presence semantics, where misplaced duplicate letters remain yellow rather than consuming a limited match count.

Given an answer string and a guess of the same length, produce one color for each guessed character:

  • "green" if the character matches the answer at the same position.
  • "yellow" if it does not match at that position but appears anywhere in the answer.
  • "red" if it does not appear anywhere in the answer.

Return the colors in guess order.

Function Signature

def color_guess(answer: str, guess: str) -> list[str]:
    ...

Constraints

  • 1 <= len(answer) == len(guess) <= 100_000
  • Inputs contain lowercase English letters.
  • This task uses simple presence semantics: yellow matches are not consumed. If a letter appears anywhere in the answer, every misplaced occurrence of that letter in the guess is yellow.

Examples

Input:  answer = "candy", guess = "dandy"
Output: ["yellow", "green", "green", "green", "green"]

Input:  answer = "candy", guess = "zandy"
Output: ["red", "green", "green", "green", "green"]

Input:  answer = "aba", guess = "bbb"
Output: ["yellow", "green", "yellow"]

Clarifications

  • Check positional equality before checking whether a character appears elsewhere.
  • The duplicate-letter rule intentionally differs from count-limited Wordle scoring.

Generate Color Feedback for a Word Guess

For equal-length lowercase strings, mark each guess position green for an exact match, yellow for a misplaced character present anywhere in the answer, or red when absent.

Hints

  1. Check exact positional equality first.
  2. Precompute answer-character membership for misplaced guesses.

Approach

A set answers whether each nonmatching guessed character occurs anywhere in the answer. Exact positional matches take priority over that membership check.

  • Time complexity: O(n)
  • Space complexity: O(1) for the fixed lowercase alphabet