Skip to content

Instantly share code, notes, and snippets.

@BiruLyu
Created May 27, 2018 03:40
Show Gist options
  • Select an option

  • Save BiruLyu/b9cb314cf0a2aa263c22ecfa06f30025 to your computer and use it in GitHub Desktop.

Select an option

Save BiruLyu/b9cb314cf0a2aa263c22ecfa06f30025 to your computer and use it in GitHub Desktop.
/**
* // 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 {
private int match(String guess, String w) {
int match = 0;
for (int i = 0; i < w.length(); i++) {
if (guess.charAt(i) == w.charAt(i)) match++;
}
return match;
}
public void findSecretWord(String[] wordlist, Master master) {
for (int i = 0, x = 0; i < 10 && x < 6; i++) {
String guess = wordlist[new Random().nextInt(wordlist.length)];
x = master.guess(guess);
List<String> wordlist2 = new ArrayList<>();
for (String w : wordlist) {
if (match(w, guess) == x) {
wordlist2.add(w);
}
}
wordlist = wordlist2.toArray(new String[wordlist2.size()]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment