LeetCode Entry
1160. Find Words That Can Be Formed by Characters
Sum of words lengths constructed by chairs
1160. Find Words That Can Be Formed by Characters easy
blog post
substack
youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/425
Problem TLDR
Sum of words lengths constructed by chairs
Intuition
Just use the char frequencies map
Approach
Some Kotlin’s API:
- groupBy
- sumBy
- all
- let
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\), can be O(1)
Code
fun countCharacters(words: Array<String>, chars: String): Int =
chars.groupBy { it }.let { freq ->
words.sumBy {
val wfreq = it.groupBy { it }
if (wfreq.keys.all { freq[it] != null
&& wfreq[it]!!.size <= freq[it]!!.size })
it.length else 0
}
}