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
|
class Solution { public void findSecretWord(String[] wordlist, Master master) { for (int i = 0, x = 0; i < 10 && x < 6; i++) { Map<String, Integer> countZeroMatches = new HashMap<>(); for (String word1: wordlist) { for (String word2: wordlist) { if (match(word1, word2) == 0) { countZeroMatches.put(word1, countZeroMatches.getOrDefault(word1, 0) + 1); } } } int min = 1000; String minZeroMatchesWord = ""; for (String word: wordlist) { if (countZeroMatches.getOrDefault(word, 0) < min) { min = countZeroMatches.getOrDefault(word, 0); minZeroMatchesWord = word; } } x = master.guess(minZeroMatchesWord); List<String> wordlist2 = new ArrayList<>(); for (String word: wordlist) { if (match(minZeroMatchesWord, word) == x) { wordlist2.add(word); } } wordlist = wordlist2.toArray(new String[0]); } } private int match(String word1, String word2) { int count = 0; for (int i = 0; i < word1.length(); i++) { if (word1.charAt(i) == word2.charAt(i)) count++; } return count; } }
|
近期评论