464. Wordle Feedback Algorithm
Wordle Feedback Algorithm

Implement Wordle feedback for a five-letter target word and a guess. Return one character for each letter in the guess, in the same order. As a follow-up, process a list of guesses with a maximum number of attempts.

Feedback Rules

  • G: The letter matches the target at the same position.
  • Y: The letter matches an unused occurrence elsewhere in the target.
  • X: No unused matching occurrence remains in the target.
  • Each occurrence in the target can be matched only once per guess. Assign all G matches first, then assign Y matches to the remaining guess positions from left to right. This rule also applies to repeated letters.

Methods

Single Guess

String generateFeedback(String targetWord, String guess)

  • targetWord: The word to be guessed.
  • guess: The submitted guess.
  • Return a five-character string containing only G, Y, and X.

Follow-Up: Multiple Attempts

List<String> generateFeedbackForAttempts(String targetWord, List<String> guesses, int maxAttempts)

  • targetWord: The target shared by all attempts.
  • guesses: The guesses to process in their given order.
  • maxAttempts: The maximum number of guesses to process.
  • Use generateFeedback for each processed guess. Each guess starts with all target letter occurrences available again.
  • Stop after a feedback of "GGGGG", after maxAttempts guesses, or when the list ends, whichever happens first.
  • Return the feedback strings in attempt order, including the successful attempt if one occurs. An empty guess list returns an empty list.

Constraints

  • targetWord.length() == 5
  • guess.length() == 5 and every entry in guesses has length 5.
  • All words contain only lowercase English letters and may contain repeated letters. Dictionary validation is not required.
  • 0 ≤ guesses.size() ≤ 10,000
  • 1 ≤ maxAttempts ≤ 10,000
  • All parameters and list entries are non-null.

Examples

Example 1: Mixed Feedback

generateFeedback(targetWord = "plant", guess = "plate")

Output: "GGGYX"

The first three letters match their positions. The letter t belongs elsewhere in the target, while e is absent.

Example 2: Repeated Letters

generateFeedback(targetWord = "abbey", guess = "bobby")

Output: "YXGXG"

The third and fifth letters are exact matches. The first b uses the remaining target b, so the fourth letter receives X. The letter o is absent.

Example 3: Stop After Success

generateFeedbackForAttempts(targetWord = "plant", guesses = ["plate", "slant", "plant", "prone"], maxAttempts = 5)

Output: ["GGGYX", "XGGGG", "GGGGG"]

The third guess finds the target, so the fourth guess is not processed.

Example 4: Attempt Limit

generateFeedbackForAttempts(targetWord = "plant", guesses = ["plate", "slant", "plant", "prone"], maxAttempts = 2)

Output: ["GGGYX", "XGGGG"]

Only the first two guesses are processed because the attempt limit is reached.



Please use Laptop/Desktop or any other large screen to add/edit code.