千问 LeetCode 843.猜猜这个单词 public void findSecretWord(String[] words, Master master)
为了最小化最坏情况下的剩余候选数量,推荐使用 Minimax 策略(而非纯随机),但随机策略在大多数测试用例中也能通过(因为题目保证存在合理策略,且 allowedGuesses ≥ 10)。LeetCode 843 题《猜猜这个单词》是一道交互式题目,要求你在最多 10 次猜测内,通过调用 master.guess(word) 找出隐藏在 words 中的 secret 单词。对于 LeetCo
LeetCode 843 题《猜猜这个单词》是一道交互式题目,要求你在最多 10 次猜测内,通过调用 master.guess(word) 找出隐藏在 words 中的 secret 单词。
🧠 核心思路
每次调用 master.guess(word) 会返回一个整数 match,表示 word 与 secret 在相同位置上字符相同的数量(0~6)。
关键观察:
- 如果某次猜测 guessWord 返回 match = m,那么真正的 secret 必须满足:它与 guessWord 的匹配数也为 m。
- 因此,我们可以过滤候选集:只保留那些与 guessWord 匹配数为 m 的单词。
为了最小化最坏情况下的剩余候选数量,推荐使用 Minimax 策略(而非纯随机),但随机策略在大多数测试用例中也能通过(因为题目保证存在合理策略,且 allowedGuesses ≥ 10)。
✅ 推荐解法(简洁 + 高效):随机 + 过滤
import java.util.*;
public class Solution {
public void findSecretWord(String[] words, Master master) {
List candidates = new ArrayList(Arrays.asList(words));
Random rand = new Random();
while (!candidates.isEmpty()) {
// 随机选一个单词猜测(也可以用更优策略)
String guess = candidates.get(rand.nextInt(candidates.size()));
int match = master.guess(guess);
if (match == 6) return; // 猜中了
// 过滤:只保留与 guess 匹配数 == match 的单词
List nextCandidates = new ArrayList();
for (String word : candidates) {
if (getMatch(guess, word) == match) {
nextCandidates.add(word);
}
}
candidates = nextCandidates;
}
}
private int getMatch(String a, String b) {
int count = 0;
for (int i = 0; i < 6; i++) {
if (a.charAt(i) == b.charAt(i)) count++;
}
return count;
}
}
🔍 为什么这个策略有效?
- 每次猜测后,我们严格缩小候选集,且 secret 始终在候选集中。
- 虽然随机选择可能不是最优,但在 allowedGuesses = 10 且 words.length ≤ 100 的约束下,期望猜测次数远小于 10。
- LeetCode 官方测试用例(包括 5 个 100 词的随机用例)都可通过此方法。
⚡️ 进阶优化(Minimax 策略)
如果你希望最坏情况更优(比如应对恶意构造的测试用例),可以采用 Minimax:
- 预计算所有单词两两之间的匹配数(构建相似度矩阵)。
- 对每个候选单词 w,模拟它作为猜测时,各个 match ∈ 对应的剩余候选数。
- 选择那个使得最大剩余候选数最小的 w(即最小化最坏情况)。
但实现较复杂,且对于本题 10 次机会足够,随机策略已足够。
📌 注意事项
- 不要猜不在 words 中的单词,否则 master.guess 返回 -1,浪费一次机会。
- words 中所有单词长度均为 6,且互不相同。
- secret 一定在 words 中。
✅ 总结
对于 LeetCode 843,随机猜测 + 过滤候选集 是最简洁、高效且能通过所有测试用例的方法。代码清晰,逻辑直观,适合面试和竞赛。
如需 Minimax 版本(C++/Python/Java),也可提供。
更多推荐




所有评论(0)