LeetCode Problem

How to Solve Guess the Word

To solve Guess the Word efficiently, start by understanding the array of words and the math behind matching letters. Use Master.guess strategically to eliminate impossible candidates and narrow down the secret word. This approach combines Array filtering, Math for match counts, and String comparison logic for a controlled interactive search.

GhostInterview Help

Need help with Guess the Word without spending extra time grinding it?

GhostInterview can read Guess the Word from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #843Array plus MathReviewed 2026-03-08
Difficulty
Hard
Primary pattern
Array plus Math
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve Guess the Word efficiently, start by understanding the array of words and the math behind matching letters. Use Master.guess strategically to eliminate impossible candidates and narrow down the secret word. This approach combines Array filtering, Math for match counts, and String comparison logic for a controlled interactive search.

Problem Statement

You are given an array of unique six-letter strings called words. One word from this array is selected as the secret word, and your goal is to identify it using limited guesses.

You can call Master.guess(word) with a word from the array, which returns the number of letters matching the secret word in the correct position. Each test case has allowedGuesses specifying the maximum number of guesses you can make to discover the secret.

Examples

Example 1

Input: secret = "acckzz", words = ["acckzz","ccbazz","eiowzz","abcczz"], allowedGuesses = 10

Output: You guessed the secret word correctly.

master.guess("aaaaaa") returns -1, because "aaaaaa" is not in wordlist. master.guess("acckzz") returns 6, because "acckzz" is secret and has all 6 matches. master.guess("ccbazz") returns 3, because "ccbazz" has 3 matches. master.guess("eiowzz") returns 2, because "eiowzz" has 2 matches. master.guess("abcczz") returns 4, because "abcczz" has 4 matches. We made 5 calls to master.guess, and one of them was the secret, so we pass the test case.

Example 2

Input: secret = "hamada", words = ["hamada","khaled"], allowedGuesses = 10

Output: You guessed the secret word correctly.

Since there are two words, you can guess both.

Constraints

  • 1 <= words.length <= 100
  • words[i].length == 6
  • words[i] consist of lowercase English letters.
  • All the strings of wordlist are unique.
  • secret exists in words.
  • 10 <= allowedGuesses <= 30

Solution Approach

Use match-count elimination

Call Master.guess on a candidate word, then remove all words from the array that cannot produce the same number of matches. This array-based pruning quickly narrows down the possibilities.

Select optimal guesses with math heuristics

Compute expected match counts for each word against the remaining array and choose the word that minimizes the worst-case remaining candidates. This math-driven strategy prevents wasted guesses.

Iterate until secret is found

Repeat guessing, evaluating match counts, and filtering the word array. Stop when Master.guess returns full matches equal to six letters, ensuring the secret word is identified within allowed guesses.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time and space complexity vary depending on the pruning strategy. Worst-case time is O(N^2) for match comparisons, and space is O(N) to store remaining candidates.

What Interviewers Usually Probe

  • Expect you to explain the elimination strategy and why filtering the array reduces guess count.
  • Demonstrate understanding of match-count math to optimize the next guess choice.
  • Clarify handling of edge cases where multiple words produce similar matches.

Common Pitfalls or Variants

Common pitfalls

  • Guessing words randomly without using match-count filtering can exceed allowed guesses.
  • Failing to account for all remaining candidates when computing the optimal guess may lead to unnecessary calls.
  • Ignoring that each guess must be in the provided words array can cause invalid submissions.

Follow-up variants

  • Increasing word length to seven or more letters to test scalable array plus math filtering.
  • Reducing allowedGuesses to force more strategic elimination choices instead of trial-and-error.
  • Changing the word array to include partial duplicates in letter patterns to test heuristic selection.

How GhostInterview Helps

  • Automatically tracks previous guesses and match counts to suggest optimal next guesses based on array filtering.
  • Highlights the best candidate words using math-based heuristics to maximize information gain per guess.
  • Detects failure modes such as repeated invalid guesses or suboptimal elimination, guiding toward correct strategies.

Topic Pages

FAQ

What is the most efficient strategy for Guess the Word?

Use array pruning based on match counts and select guesses that minimize the worst-case remaining candidates using match-count math.

Can I guess words not in the array?

No, every guess must be a word from the provided words array; otherwise Master.guess returns invalid results.

How does match-count math improve guessing?

By computing potential matches across the remaining words, you can pick guesses that eliminate the largest number of candidates each turn.

What happens if I exceed allowedGuesses?

Exceeding allowedGuesses results in failure, so strategic selection using array filtering and match-count analysis is critical.

Is Guess the Word more Array or Math focused?

It combines both: arrays store candidates and are filtered, while math determines which guess yields maximum elimination efficiency.

GhostInterview Solver

Need direct help with Guess the Word instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Guess the Word from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.