fun canConstruct(str: String, anotherStr: String): Boolean {
// ASCII frequency table
val freq = IntArray(256)
// Count characters in anotherStr
for (ch in anotherStr) {
freq[ch.code]++
}
// Check if str can be constructed
for (ch in str) {
val idx = ch.code
freq[idx]--
if (freq[idx] < 0) return false
}
return true
}
fun main() {
val str = "hello"
val anotherStr = "olehhlxyz"
println(canConstruct(str, anotherStr))
}
/*
run:
true
*/