File tree 1 file changed +33
-0
lines changed
1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* *
2
+ * // This is the Master's API interface.
3
+ * // You should not implement it, or speculate about its implementation
4
+ * interface Master {
5
+ * fun guess(word: String): Int {}
6
+ * }
7
+ */
8
+ class Solution {
9
+ private fun String.partialMatches (other : String ): Int {
10
+ return foldIndexed(0 ) { j, acc, char -> acc + (if (char == other[j]) 1 else 0 ) }
11
+ }
12
+
13
+ fun findSecretWord (words : Array <String >, master : Master ) {
14
+ words.sortBy { it.toCharArray().distinct().size }
15
+
16
+ val isRemoved = mutableSetOf<String >()
17
+
18
+ for (word in words) {
19
+ if (word in isRemoved) {
20
+ continue
21
+ }
22
+
23
+ val guessed = master.guess(word)
24
+ if (guessed == 6 ) {
25
+ return
26
+ }
27
+
28
+ isRemoved + = words.filter {
29
+ it !in isRemoved && it.partialMatches(word) != guessed
30
+ }
31
+ }
32
+ }
33
+ }
You can’t perform that action at this time.
0 commit comments