#843

Guess the Word

master · 1865 · lc hard +32 · verified · 36.9% accepted · 1,631 likes · top 15%

Description

You have an array of unique 6-letter strings words. One of them is the secret word.

A helper object Master is available. Calling Master.guess(word) where word is a 6-letter string from words returns:

- -1 if word is not in words, or

- The number of positions where word and the secret word share the same character.

Each test case imposes an allowedGuesses limit on calls to Master.guess.

Complete the task by calling Master.guess with the secret word within the allowed number of guesses. You will receive:

- "Either you took too many guesses, or you did not find the secret word." if you exceed the limit or never guess correctly, or

- "You guessed the secret word correctly." if you identify it within the limit.

Test cases are constructed so that a reasonable strategy (not brute force) can always succeed.

Example 1:

Input: secret = "acckzz", words = ["acckzz","ccbazz","eiowzz","abcczz"], allowedGuesses = 10
Output: You guessed the secret word correctly.
Explanation:
master.guess("aaaaaa") returns -1, because "aaaaaa" is not in words.
master.guess("acckzz") returns 6, because "acckzz" is secret and has all 6 matches.
master.guess("ccbazz") returns 3, because "ccbazz" has 3 matches.
master.guess("eiowzz") returns 2, because "eiowzz" has 2 matches.
master.guess("abcczz") returns 4, because "abcczz" has 4 matches.
We made 5 calls to master.guess, and one of them was the secret, so we pass the test case.

Example 2:

Input: secret = "hamada", words = ["hamada","khaled"], allowedGuesses = 10
Output: You guessed the secret word correctly.
Explanation: Since there are two words, you can guess both.

Code

1
2
3
4
5
6
7
8
9
10