Adaptive Word Guessing Minimax Strategy
Dynamic ProgrammingAuto-graded · 21 test casesLast updated
Find the minimum number of worst-case letter guesses required to isolate a hidden word against an adversarial host.
You are given a list of unique lowercase English words, where all words have equal length.
A secret word is selected from this set. You interact with an adversarial opponent by making letter guesses one turn at a time. Each turn, you choose a single lowercase letter from 'a' to 'z' that has not been guessed yet.
The opponent responds by revealing all index positions where the guessed letter appears within a secret candidate word. However, the opponent is allowed to dynamically switch the active candidate word after any turn, provided the new word matches all feedback patterns revealed in previous turns. The opponent will always choose a response that maximizes the total number of guesses you must make.
Your goal is to uniquely isolate the secret word (i.e., reduce the set of possible consistent words to exactly one).
Assuming both you and the opponent play optimally, calculate the minimum number of letter guesses required to guarantee the secret word is identified in the worst-case scenario.
Constraints
1 <= words.length <= 141 <= words[i].length <= 10- All strings in
wordsconsist of lowercase English letters ('a'through'z'). - All strings in
wordshave the exact same length. - All strings in
wordsare unique.
Examples
Example 1
Input: worst_case_guesses(['cat', 'car', 'bar'])
Output: 2
Example 2
Input: worst_case_guesses(['a', 'b', 'c'])
Output: 2
Example 3
Input: worst_case_guesses(['aa', 'ab', 'ba', 'bb'])
Output: 1
Hints
Hint 1
Framing the opponent's strategy: whenever you guess a letter, the current candidate set is partitioned into disjoint subsets according to the revealed index pattern. The opponent will always direct you toward the largest remaining branch (in terms of worst-case future turns).
Hint 2
Because you want to minimize the worst-case cost, this problem can be modeled using a minimax recurrence relation on candidate subsets.
Hint 3
Represent candidate subsets using bitmasks for fast operations and efficient state memoization.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Method Explanation
The problem can be modeled as a zero-sum, two-player game (minimax) played on candidate sets of words:
- At any point, there is a set of valid candidate words consistent with all historical feedback.
- When a letter is guessed, each candidate word produces a distinct match pattern (the list of indices where that letter appears).
- The guessed letter partitions the candidate set into disjoint subsets, one for each distinct pattern.
- The adversarial host picks the partition subset that forces the maximum number of additional guesses.
- You choose the unused letter that minimizes this worst-case outcome.
Key Implementation Details:
- Bitmask Representation: Represent candidate word subsets as bitmasks where the $i$-th bit indicates whether
words[i]is still valid. - Precomputed Grouping: For each letter
'a'–'z', pre-calculate how the original list of words partitions based on position patterns. During recursion, filter these partitions using bitwiseANDwith the active candidate mask. - Pruning: Ignore letters that do not split the active candidate set (i.e., letters yielding fewer than 2 non-empty partitions), as making such a guess wastes a move without providing new information.
- Dynamic Programming with Memoization: Store the result for state
(candidates_mask, guessed_letters_mask)using standard caching.
Complexity Analysis
- Time Complexity: $\mathcal{O}(2^N \cdot 26 \cdot N)$, where $N$ is the number of words. Since candidate sets strictly shrink at each valid step, the total number of reachable states is far smaller than $2^N$.
- Space Complexity: $\mathcal{O}(2^N)$ to store the cached states in memoization tables, along with $\mathcal{O}(N)$ recursion depth.
Python Reference Solution
from functools import lru_cache
from typing import List
def worst_case_guesses(words: List[str]) -> int:
if len(words) <= 1:
return 0
letter_masks = []
for letter_index in range(26):
groups = {}
letter = chr(ord('a') + letter_index)
for word_index, word in enumerate(words):
pattern = tuple(i for i, char in enumerate(word) if char == letter)
groups.setdefault(pattern, 0)
groups[pattern] |= 1 << word_index
letter_masks.append(tuple(groups.values()))
full_candidates = (1 << len(words)) - 1
@lru_cache(None)
def solve(candidates, guessed):
if candidates & (candidates - 1) == 0:
return 0
best = float('inf')
for letter_index, groups in enumerate(letter_masks):
bit = 1 << letter_index
if guessed & bit:
continue
parts = [group & candidates for group in groups if group & candidates]
if len(parts) <= 1:
continue
worst = max(solve(part, guessed | bit) for part in parts)
best = min(best, 1 + worst)
return best
return solve(full_candidates, 0)