Guess the Word
Problem statement
You are given an array words of distinct six-letter lowercase words and a secret word that appears in words.
A guess receives the number of positions whose letters exactly match secret. Simulate the following deterministic solver for at most 10 guesses:
- Start with every word as a possible secret.
- For each current candidate guess, partition the current candidates by feedback value from
0through6. - Choose the guess whose largest partition is smallest. Break ties by lexicographically smaller word.
- If all six positions match, return the one-based guess number. Otherwise retain only candidates with the observed feedback and continue.
Return -1 if this policy does not guess secret within ten attempts.
Function
guessesToFindSecret(words: String[], secret: String) → intExamples
Example 1
words = ["acckzz","ccbazz","eiowzz","abcczz"]secret = "acckzz"return = 1The minimax rule chooses acckzz first because every feedback bucket has size at most one.
Example 2
words = ["acckzz","ccbazz","eiowzz","abcczz"]secret = "eiowzz"return = 2The first guess acckzz receives feedback 2, leaving only eiowzz, which is guessed next.
Example 3
words = ["aaaaaa","bbbbbb","cccccc"]secret = "cccccc"return = 3Every distinct pair has feedback zero, so lexicographic tie-breaking guesses aaaaaa, then bbbbbb, then cccccc.
Constraints
1 <= words.length <= 100.- Every word has exactly six lowercase English letters.
- All words are distinct.
secretappears inwords.