Skip to content

Instantly share code, notes, and snippets.

@vishwakarma
Created August 18, 2020 14:45
Show Gist options
  • Save vishwakarma/beb9ce2de10f16a861191c522fb091bf to your computer and use it in GitHub Desktop.
Save vishwakarma/beb9ce2de10f16a861191c522fb091bf to your computer and use it in GitHub Desktop.
class Solution {
public void findSecretWord(String[] wordlist, Master master) {
Random random = new Random();
for (int i = 0; i < 10; i++) {
int idx = random.nextInt(wordlist.length);
String word = wordlist[idx];
int guess = master.guess(word);
if (guess == 6) {
break;
}
wordlist = trimList(wordlist, word, guess);
}
}
private String[] trimList(String[] wordlist, String word, int guess) {
List<String> newList = new ArrayList<>();
for (int i = 0; i < wordlist.length; i++) {
String newWord = wordlist[i];
int count = 0;
for (int c = 0; c < newWord.length() && c < word.length(); c++) {
if (newWord.charAt(c) == word.charAt(c)) {
count++;
}
}
if (count == guess) {
newList.add(newWord);
}
}
return newList.toArray(new String[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment