LeetCode 题解工作台

猜猜这个单词

给你一个由 不同 字符串组成的单词列表 words ,其中 words[i] 长度均为 6 。 words 中的一个单词将被选作秘密单词 secret 。 另给你一个辅助对象 Master ,你可以调用 Master.guess(word) 来猜单词,其中参数 word 长度为 6 且必须是 wor…

category

5

题型

code_blocks

0

代码语言

hub

3

相关题

当前训练重点

困难 · 数组·数学

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·数学 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个由 不同 字符串组成的单词列表 words ,其中 words[i] 长度均为 6words 中的一个单词将被选作秘密单词 secret 。

另给你一个辅助对象 Master ,你可以调用 Master.guess(word) 来猜单词,其中参数 word 长度为 6 且必须是 words 中的字符串。

Master.guess(word) 将会返回如下结果:

  • 如果 word 不是 words 中的字符串,返回 -1 ,或者
  • 一个整数,表示你所猜测的单词 word秘密单词 secret 的准确匹配(值和位置同时匹配)的数目。

每组测试用例都会包含一个参数 allowedGuesses ,其中 allowedGuesses 是你可以调用 Master.guess(word) 的最大次数。

对于每组测试用例,在不超过允许猜测的次数的前提下,你应该调用 Master.guess 来猜出秘密单词。最终,你将会得到以下结果:

  • 如果你调用 Master.guess 的次数大于 allowedGuesses 所限定的次数或者你没有用 Master.guess 猜到秘密单词,则得到 "Either you took too many guesses, or you did not find the secret word."
  • 如果你调用 Master.guess 猜到秘密单词,且调用 Master.guess 的次数小于或等于 allowedGuesses ,则得到 "You guessed the secret word correctly."

生成的测试用例保证你可以利用某种合理的策略(而不是暴力)猜到秘密单词。

 

示例 1:

输入:secret = "acckzz", words = ["acckzz","ccbazz","eiowzz","abcczz"], allowedGuesses = 10
输出:You guessed the secret word correctly.
解释:
master.guess("aaaaaa") 返回 -1 ,因为 "aaaaaa" 不在 words 中。
master.guess("acckzz") 返回 6 ,因为 "acckzz" 是秘密单词 secret ,共有 6 个字母匹配。
master.guess("ccbazz") 返回 3 ,因为 "ccbazz" 共有 3 个字母匹配。
master.guess("eiowzz") 返回 2 ,因为 "eiowzz" 共有 2 个字母匹配。
master.guess("abcczz") 返回 4 ,因为 "abcczz" 共有 4 个字母匹配。
一共调用 5 次 master.guess ,其中一个为秘密单词,所以通过测试用例。

示例 2:

输入:secret = "hamada", words = ["hamada","khaled"], allowedGuesses = 10
输出:You guessed the secret word correctly.
解释:共有 2 个单词,且其中一个为秘密单词,可以通过测试用例。

 

提示:

  • 1 <= words.length <= 100
  • words[i].length == 6
  • words[i] 仅由小写英文字母组成
  • words 中所有字符串 互不相同
  • secret 存在于 words
  • 10 <= allowedGuesses <= 30
lightbulb

解题思路

方法一

1
2

speed

复杂度分析

指标
时间and space complexity vary depending on the pruning strategy. Worst-case time is O(N^2) for match comparisons, and space is O(N) to store remaining candidates.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Expect you to explain the elimination strategy and why filtering the array reduces guess count.

  • question_mark

    Demonstrate understanding of match-count math to optimize the next guess choice.

  • question_mark

    Clarify handling of edge cases where multiple words produce similar matches.

warning

常见陷阱

外企场景
  • error

    Guessing words randomly without using match-count filtering can exceed allowed guesses.

  • error

    Failing to account for all remaining candidates when computing the optimal guess may lead to unnecessary calls.

  • error

    Ignoring that each guess must be in the provided words array can cause invalid submissions.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Increasing word length to seven or more letters to test scalable array plus math filtering.

  • arrow_right_alt

    Reducing allowedGuesses to force more strategic elimination choices instead of trial-and-error.

  • arrow_right_alt

    Changing the word array to include partial duplicates in letter patterns to test heuristic selection.

help

常见问题

外企场景

猜猜这个单词题解:数组·数学 | LeetCode #843 困难