DSA problem
Guess the Word
You are given an array of unique strings words where words[i] is six letters long. One of these strings will be a secret word that you must identify.
You are also given a 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:
-1ifwordis not fromwords, or- an integer representing the number of exact matches (value and position) of your guess to the secret word.
There is a parameter allowedGuesses for each test case where allowedGuesses is the maximum number of times you can call Master.guess(word).
For each test case, you should call Master.guess with the secret word without exceeding the maximum number of allowed guesses. You will get:
- "Either you took too many guesses, or you did not find the secret word." if you called
Master.guessmore thanallowedGuessestimes or if you did not callMaster.guesswith the secret word, or - "You guessed the secret word correctly." if you called
Master.guesswith the secret word with the number of calls not exceedingallowedGuesses.
Examples
Example 1
Input:
secret = "acckzz", words = ["acckzz","ccbazz","eiowzz","abcczz"], allowedGuesses = 10Output:
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. master.guess("eiowzz") returns 2. master.guess("abcczz") returns 4. We made 5 calls and found the secret, so we pass.
Example 2
Input:
secret = "hamada", words = ["hamada","khaled"], allowedGuesses = 10Output:
You guessed the secret word correctly.Explanation: The secret word is "hamada"; guessing it within the allowed number of calls passes the test.
Constraints
- 1 <= words.length <= 100
- words[i].length == 6
- words[i] consist of lowercase English letters.
- All the strings of words are unique.
- secret exists in words.
- 10 <= allowedGuesses <= 30
3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.