@@ -0,0 +1,3021 @@
+package InteractiveProblem;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Random;%0A%0Apublic class GuessTheWord %7B%0A%0A    //Difficulty: Hard%0A    //TAG: Airbnb%0A    //TAG: interactive problem%0A    //TODO: https://leetcode.com/problems/guess-the-word/%0A%0A    /**%0A     * 843. Guess the Word%0A     * This problem is an interactive problem new to the LeetCode platform.%0A     *%0A     * 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.%0A     *%0A     * 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.%0A     *%0A     * 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.%0A     *%0A     * 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.%0A     *%0A     * 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.%0A     *%0A     * Example 1:%0A     * Input: secret = %22acckzz%22, wordlist = %5B%22acckzz%22,%22ccbazz%22,%22eiowzz%22,%22abcczz%22%5D%0A     *%0A     * Explanation:%0A     *%0A     * master.guess(%22aaaaaa%22) returns -1, because %22aaaaaa%22 is not in wordlist.%0A     * master.guess(%22acckzz%22) returns 6, because %22acckzz%22 is secret and has all 6 matches.%0A     * master.guess(%22ccbazz%22) returns 3, because %22ccbazz%22 has 3 matches.%0A     * master.guess(%22eiowzz%22) returns 2, because %22eiowzz%22 has 2 matches.%0A     * master.guess(%22abcczz%22) returns 4, because %22abcczz%22 has 4 matches.%0A     *%0A     * We made 5 calls to master.guess and one of them was the secret, so we pass the test case.%0A     */%0A%0A    /*%0A    Solution:%0A    Interactive problem is that, cannot guarantee the answer is 100%25 correct, key point is the approach, try to make the%0A    solution be as accuracy as possible, by solution iterations%0A     */%0A%0A    public void findSecretWord(String%5B%5D wordlist, Master master) %7B%0A        for (int i = 0, x = 0; i %3C 10 && x %3C 6; ++i) %7B%0A            String guess = wordlist%5Bnew Random().nextInt(wordlist.length)%5D;%0A            x = master.guess(guess);%0A            List%3CString%3E wordlist2 = new ArrayList%3C%3E();%0A            for (String w : wordlist)%0A                if (match(guess, w) == x)%0A                    wordlist2.add(w);%0A            wordlist = wordlist2.toArray(new String%5Bwordlist2.size()%5D);%0A        %7D%0A    %7D%0A%0A    private int match(String a, String b) %7B%0A        int matches = 0;%0A        for (int i = 0; i %3C a.length(); ++i) if (a.charAt(i) == b.charAt(i)) matches ++;%0A        return matches;%0A    %7D%0A%0A%0A%0A%7D%0A%0Ainterface Master %7B%0A    public int guess(String word);%0A%7D%0A

843. Guess the Word