> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Guess the Word Python Solution with Tests

> Tested Python solution for LeetCode 843 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 843, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [String](/catalog/topics/string), Minimax, [Interactive](/catalog/topics/interactive), [Game Theory](/catalog/topics/game-theory). [View on LeetCode](https://leetcode.com/problems/guess-the-word/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 843   # by problem number
lcpy gen -s guess_the_word   # by problem name
```

## Problem

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`, or
* an integer representing the number of exact matches (value and position) of your guess to the secret word.

There 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).

### Examples

```
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 words.
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.
```

```
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 <= 100`
* `words[i].length == 6`
* `words[i]` consist of lowercase English letters.
* All the strings of `words` are **unique**.
* `secret` exists in `words`.
* `10 <= allowedGuesses <= 30`

**Follow up:** What is the minimum number of guesses needed to guarantee finding the secret word?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/guess_the_word/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/guess_the_word/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


def match_count(a: str, b: str) -> int:
    return sum(x == y for x, y in zip(a, b, strict=True))


class Master:
    # Test-harness API: backs the guess interface with the hidden secret word
    def __init__(self, secret: str, words: list[str], allowed_guesses: int) -> None:
        self.secret = secret
        self.wordset = set(words)
        self.allowed_guesses = allowed_guesses
        self.calls = 0
        self.found = False

    def guess(self, word: str) -> int:
        self.calls += 1
        if word not in self.wordset:
            return -1
        matches = sum(a == b for a, b in zip(word, self.secret, strict=True))
        if matches == len(self.secret):
            self.found = True
        return matches

    def outcome(self) -> bool:
        return self.found and self.calls <= self.allowed_guesses


class Solution:
    # Time: O(g * n^2) for n candidates over g guesses
    # Space: O(n)
    def find_secret_word(self, words: list[str], master: Master) -> None:
        candidates = list(words)
        while len(candidates) > 1:
            guess = min(candidates, key=lambda w: self._worst_bucket(w, candidates))
            matches = master.guess(guess)
            candidates = [w for w in candidates if match_count(w, guess) == matches]
        if candidates:
            master.guess(candidates[0])

    def _worst_bucket(self, word: str, candidates: list[str]) -> int:
        counts = Counter(match_count(c, word) for c in candidates)
        return max(counts.values())
```

## Complexity

| Time | Space |
| - | - |
| O(g \* n^2) for n candidates over g guesses | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.