You are given an array of unique strings words where words[i] is six letters long. One word of words was chosen as a secret word.
You are also given the helper object Master. You may call Master.guess(word) where word is a six-letter-long string, and it must be from words. Master.guess(word) returns:
-1 if word is not from words, orThere is a parameter allowedGuesses for each test case where allowedGuesses is the maximum number of times you can call Master.guess(word).
For each test case, you should call Master.guess with the secret word without exceeding the maximum number of allowed guesses. You will get:
"Either you took too many guesses, or you did not find the secret word." if you called Master.guess more than allowedGuesses times or if you did not call Master.guess with the secret word, or"You guessed the secret word correctly." if you called Master.guess with the secret word with the number of calls to Master.guess less than or equal to allowedGuesses.The test cases are generated such that you can guess the secret word with a reasonable strategy (other than using the bruteforce method).
Example 1:
Input: secret = "acckzz", words = ["acckzz","ccbazz","eiowzz","abcczz"], allowedGuesses = 10
Output: You guessed the secret word correctly.
Explanation:
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. Explanation: Since there are two words, you can guess both.
Constraints:
1 <= words.length <= 100words[i].length == 6words[i] consist of lowercase English letters.wordlist are unique.secret exists in words.10 <= allowedGuesses <= 30Problem Overview: You receive a list of possible 6-letter words. One of them is the secret. Each guess returns the number of characters that match the secret word at the same position. The goal is to identify the secret word within 10 guesses by strategically choosing which word to query.
Approach 1: Minimax Strategy (O(n^2 * L) time, O(n) space)
This approach treats the problem like an adversarial game. For every candidate word in the list, compute how it partitions the remaining words based on the number of positional matches (0–6). The worst-case bucket size after a guess represents how much uncertainty remains. The minimax rule picks the word whose largest bucket is smallest, minimizing the worst-case remaining candidates. Implementation typically builds a frequency array for match counts and chooses the guess with the smallest maximum partition. After each guess, filter the word list to keep only words that produce the same match count with the guess as reported by Master.guess(). This process continues until the secret is found or guesses are exhausted. Time complexity is O(n^2 * L) because each pair of words may be compared to count matches (with L = 6), and space complexity is O(n) for maintaining candidate lists. This approach relies heavily on efficient comparison logic and careful pruning of the candidate set.
Approach 2: Randomized Guessing with Strategy (O(n^2 * L) expected time, O(n) space)
A simpler alternative randomly selects a word from the remaining candidates and uses the returned match count to eliminate inconsistent words. After a guess, iterate through the word list and keep only those words whose positional match count with the guessed word equals the response from the master API. The filtering step ensures the candidate pool shrinks every round. Although the selection is random, repeated filtering often converges quickly because the search space drops sharply after each guess. Expected complexity remains around O(n^2 * L) due to repeated comparisons during filtering. This method is easier to implement and often passes constraints, but it lacks the worst-case guarantees provided by the minimax approach.
Both approaches rely on repeatedly comparing words character-by-character and maintaining a shrinking candidate set, which makes the problem closely related to techniques from array processing and string comparison. The decision strategy—especially in the minimax version—mirrors concepts from game theory, where you minimize the opponent's best outcome.
Recommended for interviews: The minimax strategy is the expected solution. It demonstrates the ability to reason about worst-case partitions and adversarial search spaces. A randomized filtering approach can show initial understanding, but the minimax strategy signals stronger algorithmic thinking and better control over worst-case behavior.
In this approach, we utilize a minimax strategy to minimize the possible size of the word list with each guess. For every guess, we divide the list based on the number of matches it has with other words in the list. The goal is to eliminate as many potential candidates as possible with each guess to find the secret word more efficiently.
We first count matches between each pair of words and use a heuristic to maximize the minimum size of the elimination set. The strategy involves selecting the word which, when guessed, minimizes the worst-case number of words remaining in the list.
Time Complexity: O(N^2) where N is the number of words. Space Complexity: O(N).
An alternative approach involves a randomized method that guesses based on the frequency of word intersections. Utilize a strategy that considers how often words share the same characters positionally, choosing those guesses that maximize intersection frequencies to steadily isolate the secret word.
This Python solution randomly selects a word and guesses it. It only keeps words that have the same matching character count as reported by the guess to continuously narrow down the possibilities.
Time Complexity: O(N) for each guess where N is the length of words. Space Complexity: O(N) for maintaining words.
| Approach | Complexity |
|---|---|
| Approach 1: Minimax Strategy | Time Complexity: O(N^2) where N is the number of words. Space Complexity: O(N). |
| Approach 2: Randomized Guessing with Strategy | Time Complexity: O(N) for each guess where N is the length of words. Space Complexity: O(N) for maintaining words. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Randomized Guessing with Filtering | O(n^2 * L) expected | O(n) | Quick implementation when constraints are small and worst-case guarantees are not critical |
| Minimax Strategy | O(n^2 * L) | O(n) | Interview-preferred solution that minimizes worst-case candidate set after each guess |
LeetCode 843. Guess the Word • Happy Coding • 10,818 views views
Watch 9 more video solutions →Practice Guess the Word with our built-in code editor and test cases.
Practice on FleetCode