843. Guess the Word Hard
1/**
2 * [843] Guess the Word
3 *
4 * 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.
5 * 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:
6 *
7 * -1 if word is not from words, or
8 * an integer representing the number of exact matches (value and position) of your guess to the secret word.
9 *
10 * There is a parameter allowedGuesses for each test case where allowedGuesses is the maximum number of times you can call Master.guess(word).
11 * For each test case, you should call Master.guess with the secret word without exceeding the maximum number of allowed guesses. You will get:
12 *
13 * "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
14 * "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.
15 *
16 * The test cases are generated such that you can guess the secret word with a reasonable strategy (other than using the bruteforce method).
17 *
18 * Example 1:
19 *
20 * Input: secret = "acckzz", words = ["acckzz","ccbazz","eiowzz","abcczz"], allowedGuesses = 10
21 * Output: You guessed the secret word correctly.
22 * Explanation:
23 * master.guess("aaaaaa") returns -1, because "aaaaaa" is not in wordlist.
24 * master.guess("acckzz") returns 6, because "acckzz" is secret and has all 6 matches.
25 * master.guess("ccbazz") returns 3, because "ccbazz" has 3 matches.
26 * master.guess("eiowzz") returns 2, because "eiowzz" has 2 matches.
27 * master.guess("abcczz") returns 4, because "abcczz" has 4 matches.
28 * We made 5 calls to master.guess, and one of them was the secret, so we pass the test case.
29 *
30 * Example 2:
31 *
32 * Input: secret = "hamada", words = ["hamada","khaled"], allowedGuesses = 10
33 * Output: You guessed the secret word correctly.
34 * Explanation: Since there are two words, you can guess both.
35 *
36 *
37 * Constraints:
38 *
39 * 1 <= words.length <= 100
40 * words[i].length == 6
41 * words[i] consist of lowercase English letters.
42 * All the strings of wordlist are unique.
43 * secret exists in words.
44 * 10 <= allowedGuesses <= 30
45 *
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/guess-the-word/
50// discuss: https://leetcode.com/problems/guess-the-word/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54/**
55 * // This is the Master's API interface.
56 * // You should not implement it, or speculate about its implementation
57 * struct Master;
58 * impl Master {
59 * fn guess(word:String)->int;
60 * };
61 */
62
63impl Solution {
64 pub fn find_secret_word(words: Vec<String>, master: &Master) {
65
66 }
67}
68
69// submission codes end
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 #[test]
76 fn test_843() {
77 }
78}
79
Back
© 2025 bowen.ge All Rights Reserved.