559. Guess the Word (Deterministic Strategy)

HardBinary SearchGreedySimulation

You are given a list of equal-length words and a secret word from that list. You repeatedly guess words; each guess returns the number of positions that exactly match the secret. Using the deterministic strategy of always guessing the first remaining candidate and then keeping only candidates whose match count against that guess equals the secret's match count, return the number of guesses made until the secret is guessed. The input is JSON {words, secret}.

Input: JSON {words, secret}.

Output: Integer — the number of guesses made.

Examples

Example 1
Input: {"words":["abc","abd","xyz","abe"],"secret":"abe"}
Output: 3
Explanation: Filtering by match count narrows to the secret in 3 guesses.
Example 2
Input: {"words":["aa","bb","cc"],"secret":"bb"}
Output: 2
Explanation: Two guesses isolate the secret.
Example 3
Input: {"words":["abc"],"secret":"abc"}
Output: 1
Explanation: One guess.

Constraints

Asked by

GoogleAppleBloombergAmazonMetaMicrosoft
Solve this problem in the editor →