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.

思路:这道题说是有一个单词数组 wordlist,其中有一个单词是需要被猜到的单词 secret,现在有一个 api 函数 guess,可以返回猜的单词和目标单词之间的匹配个数。现在每个 test case 有 10 次机会去猜目标单词,假如调用 api 的次数不超过 10 次,并猜中了目标单词的话,就可以通过测试。首先,由于需要尽可能少的调用 api,所以线性的一个一个的对每个单词调用 api 是不可取的,因为假如目标单词在最后一个,且单词数组长度超过 10 个,就会失败。这样的话可以随机取一个单词来检测,调用 api 后会得到一个次数 cnt,表示当前单词和目标单词之间的匹配个数。接下来怎么办呢?需要过滤一遍单词数组,自己写一个类似于 api 的函数,返回任意两个单词之间的匹配个数,这样就可以 filter 整个单词数组了,因为藏在普通单词中的目标单词跟当前单词调用 match 函数的返回值一定还是 cnt,当然也会有其他的单词同样返回 cnt,不过没关系,还是能滤去一大波不相干的单词,重复这个步骤,直到 cnt 正好为6停止,因为题目中说了单词的长度就是6,

核心思想:就是随机选取一个词guess之后,因为guess是跟正确答案比较,那么再用这个词去扫描所有的单词,把相同count == word的candidate全部选出来,这样可以踢掉很多不是count的单词,也就是踢掉跟正确答案不那么匹配的单词,然后继续以上步骤,直到count == 6,也就是单词长度6为止;

/**
 * // This is the Master's API interface.
 * // You should not implement it, or speculate about its implementation
 * interface Master {
 *     public int guess(String word) {}
 * }
 */
class Solution {
    public void findSecretWord(String[] wordlist, Master master) {
        if(wordlist == null || wordlist.length == 0) {
            return;
        }
        Random random = new Random();
        int count = 0;
        List<String> wlist = Arrays.asList(wordlist);
        
        for(int i = 0, cnt = 0; i < 10 && cnt < 6; i++) {
            int n = wlist.size();
            String word = wlist.get(random.nextInt(n));
            cnt = master.guess(word);
            
            List<String> newlist = new ArrayList<>();
            for(String candidate : wlist) {
                if(getDistance(candidate, word) == cnt) {
                    newlist.add(candidate);
                } 
            }
            wlist = newlist;
        }
    }
    
    private int getDistance(String word, String target) {
        int count = 0;
        for(int i = 0; i < word.length(); i++) {
            if(word.charAt(i) == target.charAt(i)) {
                count++;
            }
        }
        return count;
    }
}
发布了673 篇原创文章 · 获赞 13 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/u013325815/article/details/105221311