Advertisement

Guess the Word - LeetCode 843 Solution

Guess the Word - Complete Solution Guide

Guess the Word is LeetCode problem 843, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The "Guess the Word" problem is an interactive game where you need to guess a secret six-letter word from a given list of unique six-letter words. You are provided with a `Master` object that allows you to make guesses. The `Master.guess(word)` method returns the number of exact matches (same letter at the same position) between your guess and the secret word. The goal is to guess the secret word within a limited number of guesses (specified by `allowedGuesses`). The problem guarantees that the secret word exists in the given word list and that a reasonable strategy exists to find it without brute-forcing all possibilities.

Solution Approach

The provided solution employs a minimax-like heuristic approach. It iteratively guesses a word from the remaining candidate words. The word selected is the one that minimizes the maximum number of words that could potentially remain after a guess and match evaluation. Specifically, for each candidate word, it calculates how many words would remain for each possible match count (0 to 6). Then it picks the candidate with the minimum value of the largest group. The candidate is guessed, and the number of matches is obtained from the `Master` object. The candidate list is then filtered to only contain words that have the same number of matches as the guess.

Step-by-Step Algorithm

  1. Step 1: Initialize the candidate list with the provided word list.
  2. Step 2: Iterate up to 30 times (the maximum allowed guesses).
  3. Step 3: If the candidate list is empty, return (secret word found or no word satisfies constraints).
  4. Step 4: For each candidate word, calculate a score that represents the maximum number of words that would be left if we guessed that candidate and it has some number of matches with secret word.
  5. Step 5: Select the candidate word with the minimum score (minimax heuristic).
  6. Step 6: Make a guess using `master.guess(guess_word)`.
  7. Step 7: If the guess matches the secret word (matches_count == 6), return.
  8. Step 8: Filter the candidate list to only include words that have the same number of matches with the guessed word as the `master.guess` method returned.
  9. Step 9: Repeat from step 2.

Key Insights

  • Insight 1: The most important insight is to reduce the search space effectively after each guess. This involves filtering the remaining candidate words based on the number of matches with the current guess.
  • Insight 2: Choosing the 'best' guess in each iteration is crucial for efficient reduction of the search space. A good strategy involves minimizing the maximum number of words that could remain after making the guess.
  • Insight 3: The constraint on the number of guesses (10 <= allowedGuesses <= 30) suggests that a heuristic approach, rather than a perfect algorithm, is sufficient and expected.

Complexity Analysis

Time Complexity: O(N^2)

Space Complexity: O(N)

Topics

This problem involves: Array, Math, String, Interactive, Game Theory.

Companies

Asked at: Dropbox, Verily, Verkada.