843. Guess the Word

This problem is an interactive problem new to the LeetCode platform.

We are given a word list of unique words, each word is 6 letters long, and one word in this list is chosen as secret.

You may call `master.guess(word)` to guess a word.  The guessed word should have type `string` and must be from the original list with 6 lowercase letters.

This function returns an `integer` type, representing the number of exact matches (value and position) of your guess to the secret word.  Also, if your guess is not in the given wordlist, it will return `-1` instead.

For each test case, you have 10 guesses to guess the word. At the end of any number of calls, if you have made 10 or less calls to `master.guess` and at least one of these guesses was the secret, you pass the testcase.

Besides the example test case below, there will be 5 additional test cases, each with 100 words in the word list.  The letters of each word in those testcases were chosen independently at random from `'a'` to `'z'`, such that every word in the given word lists is unique.

```Example 1:
Input: secret = "acckzz", wordlist = ["acckzz","ccbazz","eiowzz","abcczz"]

Explanation:

`master.guess("aaaaaa")` returns -1, because `"aaaaaa"` is not in wordlist.
`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.
```

Note:  Any solutions that attempt to circumvent the judge will result in disqualification.

843. Guess the Word
``````struct Solution;

use std::cell::Cell;
use std::collections::HashSet;
use std::iter::FromIterator;

fn exact_matches(a: &str, b: &str) -> usize {
a.chars().zip(b.chars()).filter(|(a, b)| a == b).count()
}

impl Solution {
fn find_secret_word(words: Vec<String>, master: &Master) {
let n = words.len();
let mut matrix: Vec<Vec<usize>> = vec![vec![0; n]; n];
let mut excluded: Vec<bool> = vec![false; n];
for i in 0..n {
for j in i + 1..n {
let count = exact_matches(&words[i], &words[j]);
matrix[i][j] = count;
matrix[j][i] = count;
}
}

for _ in 0..10 {
let mut id = rng.gen_range(0, n);
while excluded[id] {
id = rng.gen_range(0, n);
}
let count = master.guess(words[id].to_string()) as usize;
if count == 6 {
break;
}
for i in 0..n {
if matrix[id][i] < count {
excluded[i] = true;
}
}
}
}
}

struct Master {
list: HashSet<String>,
secret: String,
call_count: Cell<usize>,
guessed: Cell<bool>,
}

impl Master {
fn new(secret: String, words: Vec<String>) -> Self {
let call_count = Cell::new(0);
let guessed = Cell::new(false);
let list: HashSet<String> = HashSet::from_iter(words);
Master {
list,
secret,
call_count,
guessed,
}
}
fn guess(&self, word: String) -> i32 {
self.call_count.set(self.call_count.get() + 1);
if word == self.secret {
self.guessed.set(true);
}
if self.list.contains(&word) {
exact_matches(&self.secret, &word) as i32
} else {
-1
}
}
fn pass(&self) -> bool {
self.call_count.get() <= 10 && self.guessed.get()
}
}

#[test]
fn test() {
let secret = "acckzz".to_string();
let wordlist = vec_string!["acckzz", "ccbazz", "eiowzz", "abcczz"];
let master = Master::new(secret, wordlist.to_vec());
Solution::find_secret_word(wordlist, &master);
assert_eq!(master.pass(), true);
}
``````