-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0843-guess-the-word.js
56 lines (52 loc) · 2.05 KB
/
0843-guess-the-word.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* 843. Guess the Word
* https://leetcode.com/problems/guess-the-word/
* Difficulty: Hard
*
* 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.
*
* 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:
* - -1 if word is not from words, 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.guess more than allowedGuesses times or if you did not call Master.guess with the
* secret word, or
* - "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.
*
* The test cases are generated such that you can guess the secret word with a reasonable
* strategy (other than using the bruteforce method).
*/
/**
* @param {string[]} words
* @param {Master} master
* @return {void}
*/
var findSecretWord = function(words, master) {
for (let attempt = 0; attempt < 30; attempt++) {
const randomWord = words[Math.floor(Math.random() * words.length)];
const matchCount = master.guess(randomWord);
if (matchCount === 6) return;
words = words.filter(word =>
matchCount === -1
? countMatches(word, randomWord) === 0
: countMatches(word, randomWord) === matchCount
);
}
function countMatches(word1, word2) {
let matches = 0;
for (let i = 0; i < word1.length; i++) {
if (word1[i] === word2[i]) matches++;
}
return matches;
}
};