-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0691-stickers-to-spell-word.kt
48 lines (40 loc) · 1.43 KB
/
0691-stickers-to-spell-word.kt
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
class Solution {
fun minStickers(stickers: Array<String>, target: String): Int {
val stickCount = HashMap<String, MutableMap<Char, Int>>().apply {
stickers.forEach { s ->
this[s] = s.groupingBy { it }
.eachCount()
.toMutableMap()
}
}
val dp = HashMap<String, Int>()
fun dfs(t: String, stick: MutableMap<Char, Int>): Int {
dp[t]?.let {
return it
}
var res = if (stick.isEmpty()) 0 else 1
val remainT = StringBuilder()
for (c in t) {
if (c in stick && stick[c]!! > 0) {
stick[c] = stick[c]!! - 1
} else {
remainT.append(c)
}
}
if (remainT.length > 0) {
val remainTS = remainT.toString()
var used = Integer.MAX_VALUE
for (s in stickCount.values) {
if (remainTS[0]!! !in s)
continue
used = minOf(used, dfs(remainTS, s.toMutableMap()))
}
dp[remainTS] = used
res = if (used == Integer.MAX_VALUE ) Integer.MAX_VALUE else res + used
}
return res
}
val res = dfs(target, mutableMapOf<Char, Int>())
return if (res == Integer.MAX_VALUE) -1 else res
}
}