Bounded Word Guess Solver
Problem statement
Simulate a Wordle-style solver for a five-letter secret. Its ordered list potentialSecrets contains the true secret. The solver may make at most maxGuesses guesses.
- Choose the first word in the current list of potential secrets.
- If that guess equals the secret, return
true. - Otherwise, generate five-character G/Y/X feedback against the secret. G means an exact position match, Y means an unmatched copy exists elsewhere, and X means no copy remains.
- Keep only words that would produce exactly the same feedback for this guess if they were the secret. Keep their original order, then repeat.
For this exercise, assume feedback assigns all exact matches first, then assigns yellow matches from left to right using each remaining secret letter at most once. Duplicate list entries are allowed and keep their order when retained.
Return false when the attempt budget ends before the solver guesses the secret. A zero budget makes no guesses. The supplied secret lets the practice runner simulate feedback; the choice of each guess must depend only on the current potential list, not on looking up the secret directly.
Complete canSolveWithinGuesses(secret, potentialSecrets, maxGuesses). Model the list and attempt budget as solver state, and reuse a feedback helper for both observed and hypothetical feedback.
Function
canSolveWithinGuesses(secret: String, potentialSecrets: String[], maxGuesses: int) → booleanExamples
Example 1
secret = "house"potentialSecrets = ["crane","mould","house"]maxGuesses = 2return = trueThe first guess is crane, which gives XXXXG against house. Mould would give XXXXX for crane, so it is removed. House is the only remaining word, and the second guess succeeds.
Example 2
secret = "house"potentialSecrets = ["crane","stole","house"]maxGuesses = 2return = falseThe first guess crane gives XXXXG. Both stole and house produce that feedback for crane, so both remain in that order. The second guess is stole, which gives YXYXG against house. The budget ends before the solver can guess house.
Example 3
secret = "apple"potentialSecrets = ["apple"]maxGuesses = 0return = falseA zero budget permits no guesses, even when the secret is the first potential word.
Constraints
secret.length == 5.1 <= potentialSecrets.length <= 1000.- Every potential word has length 5.
- The secret and every potential word contain only lowercase English letters.
- The secret occurs in potentialSecrets.
0 <= maxGuesses <= 100000.