Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiahliu committed Dec 16, 2024
1 parent 5b0b69e commit 5d96b81
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/main/kotlin/p32xx/Problem3291.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package p32xx

import util.expect

fun main() {
class Solution {
fun minValidStrings(words: Array<String>, target: String): Int {
class Trie(val depth: Int) {
val children = arrayOfNulls<Trie>(26)
}

val root = Trie(0)
words.forEach {
var current = root
it.forEach { ch ->
current = current.children[ch - 'a'] ?: Trie(current.depth + 1).also {
current.children[ch - 'a'] = it
}
}
}

var dp = hashMapOf(root to 1)

target.forEach { ch ->
val newDp = hashMapOf<Trie, Int>()
var min = Int.MAX_VALUE
dp.forEach { (node, count) ->
node.children[ch - 'a']?.also {
newDp[it] = count
min = minOf(min, count)
}
}

if (min < Int.MAX_VALUE) {
newDp[root] = min + 1
}

dp = newDp
}

return dp.values.minOrNull() ?: -1
}
}

expect {
Solution().minValidStrings(
arrayOf(
"abc", "aaaaa", "bcdef"
), "aabcdabc"
)
}
}

0 comments on commit 5d96b81

Please sign in to comment.