g0901_1000.s0916_word_subsets.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0901_1000.s0916_word_subsets
// #Medium #Array #String #Hash_Table #2023_04_16_Time_397_ms_(88.89%)_Space_45_MB_(55.56%)
class Solution {
fun wordSubsets(words1: Array, words2: Array): List {
val l1: MutableList = ArrayList()
val target = IntArray(26)
for (s1 in words2) {
val temp = IntArray(26)
for (ch1 in s1.toCharArray()) {
temp[ch1.code - 'a'.code]++
target[ch1.code - 'a'.code] =
target[ch1.code - 'a'.code].coerceAtLeast(temp[ch1.code - 'a'.code])
}
}
for (s1 in words1) {
val count = IntArray(26)
for (ch1 in s1.toCharArray()) {
count[ch1.code - 'a'.code]++
}
if (checkIt(target, count)) {
l1.add(s1)
}
}
return l1
}
private fun checkIt(target: IntArray, count: IntArray): Boolean {
for (i in 0..25) {
if (count[i] < target[i]) {
return false
}
}
return true
}
}