Skip to content

Latest commit

 

History

History
200 lines (158 loc) · 5.86 KB

843.Guess-the-Word.md

File metadata and controls

200 lines (158 loc) · 5.86 KB

843. Guess the Word


题目地址

https://leetcode.com/problems/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.

代码

Approach #1 Minmax Guess

我们的目标是尽可能减少下一次的wordlist的大小。因此,对于每一个可能的word,我们计算出以该word作为当前guess,最大的matchNum (相当于以当当前word作为guess最坏的情况),并选择最小的 max_matchNum 作为最优的guess。

更详细来说,在给定猜词范围的条件下,避免worst case的情况,应该是看某个词和其他所有词的match数为0,1,2,3,4,5这6个结果中的max值,然后找出这个max值最小的单词,作为guess。

/**
 * // 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) { 
    for (int i = 0, x = 0; i < 10 && x < 6; i++) {
      // 1. 根据频次记录不相似的单词
      HashMap<String, Integer> count = new HashMap<>();
      for (String w1: wordlist) {
        for (String w2: wordlist) {
          if (match(w1, w2) == 0) {
            count.put(w1, count.getOrDefault(w1, 0) + 1);
          }
        }
      }
      
      // 2. 筛选频次最少的单词
      String guess = "";
      int min0 = 100;
      for (String w: wordlist) {
        if (count.getOrDefault(w, 0) < min0) {
          guess = w;
          min0 = count.getOrDefault(w, 0);
        }
      }
      
      // 3. 选取match数相同的wordlist进入下一回合迭代
      x = master.guess(guess);
      List<String> wordlist2 = new ArrayList<String>();
      for (String w: wordlist) {
        if (match(guess, w) == x) {
          wordlist2.add(w);
        }
      }
      wordlist = wordlist2.toArray(new String[0]);
    }
  }
  
  public int match(String a, String b) {
    int matches = 0;
    for (int i = 0; i < a.length(); i++) {
      if (a.charAt(i) == b.charAt(i)) {
        matches++;
      }
    }
    return matches;
  }
}

Approach #2 Minimax with Heuristic

Time: O(1) && Space: O(1)

/**
 * // 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 {
  int[][] H;
  public void findSecretWord(String[] wordlist, Master master) {
	int N = wordlist.length;
    H = new int[N][N];
    for (int i = 0; i < N; i++) {
      for (int j = i; j < N; j++) {
        int match = 0;
        for (int k = 0; k < 6; k++) {
          if (wordlist[i].charAt(k) == wordlist[j].charAt(k)) {
            match++;
          }
        }
        H[i][j] = match;
      }
    }
    
    List<Integer> possible = new ArrayList();
    List<Integer> path = new ArrayList();
    for (int i = 0; i < N; i++) {
      possible.add(i);
    }
    while (!possible.isEmpty()) {
      int guess = solve(possible, path);
      int matches = master.guess(wordlist[guess]);
      if (matches == wordlist[0].length()) {
        return;
      }
      List<Integer> possible2 = new ArrayList();
      for (Integer j: possible) {
        if (H[guess][j] == matches) {
          possible2.add(j);
        }
      }
      possible = possible2;
      path.add(guess);
    }
  }
  
  public int solve(List<Integer> possible, List<Integer> path) {
    if (possible.size() <= 2) {
      return possible.get(0);
    }
    List<Integer> ansgrp = possible;
    int ansguess = -1;
    
    for (int guess = 0; guess < H.length; guess++) {
      if (!path.contains(guess)) {
        ArrayList<Integer>[] groups = new ArrayList[7];
        for (int i = 0; i < 7; i++) {
          groups[i] = new ArrayList<Integer>();
        }
        for (Integer j: possible) {
          if (j != guess) {
            groups[H[guess][j]].add(j);
          }
        }
        
        ArrayList<Integer> maxgroup = groups[0];
        for (int i = 0; i < 7; i++) {
          if (groups[i].size() > maxgroup.size()) {
            maxgroup = groups[i];
          }
        }
        if (maxgroup.size() < ansgrp.size()) {
          ansgrp = maxgroup;
          ansguess = guess;
        }
      }
    }
    
    return ansguess;
  }
}