-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuessTheWord.java
72 lines (61 loc) · 1.98 KB
/
GuessTheWord.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.gkonovalov.problems.gametheory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
/**
* Created by Georgiy Konovalov on 8/08/2023.
* <p>
* @see <a href="https://leetcode.com/problems/guess-the-word/">
* [Hard][843] - Guess the Word
* </a>
* </p>
* Runtime Complexity: O(n) for {@code findSecretWord}.
* Space Complexity: O(n) for {@code findSecretWord}.
*/
public class GuessTheWord {
public static void main(String[] args) {
findSecretWord(new String[]{"acckzz", "ccbazz", "eiowzz", "abcczz"}, new Master() {
@Override
public int guess(String word) {
return getMatches(word, "eiowzz");
}
});
}
public static void findSecretWord(String[] words, Master master) {
List<String> candidates = new ArrayList<>(Arrays.asList(words));
Random rnd = new Random();
while (!candidates.isEmpty()) {
String word = candidates.get(rnd.nextInt(candidates.size()));
int matches = master.guess(word);
if (matches == word.length()) {
return;
}
candidates = filterCandidates(candidates, word, matches);
}
}
private static List<String> filterCandidates(List<String> candidates, String word, int matches) {
List<String> filtered = new ArrayList<>();
for (String word2 : candidates) {
if (word.equals(word2)) {
continue;
}
if (getMatches(word, word2) == matches) {
filtered.add(word2);
}
}
return filtered;
}
private static int getMatches(String a, String b) {
int count = 0;
for (int i = 0; i < Math.min(a.length(), b.length()); i++) {
if (a.charAt(i) == b.charAt(i)) {
count++;
}
}
return count;
}
interface Master {
int guess(String word);
}
}