Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 795ab56

Browse files
committed
Time: 106 ms (63.64%), Space: 46 MB (18.18%) - LeetHub
1 parent 6528be8 commit 795ab56

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}

0 commit comments

Comments
 (0)