https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/description/ Easy

Решение

fun countCharacters(words: Array<String>, chars: String): Int {
    val charsCount = IntArray(26)
    for (c in chars) charsCount[c - 'a']++  // считаем доступные символы

    var result = 0
    wordLoop@ for (word in words) {
        val wordCount = IntArray(26)
        for (c in word) {
            wordCount[c - 'a']++
            if (wordCount[c - 'a'] > charsCount[c - 'a']) continue@wordLoop  // символов не хватает
        }
        result += word.length  // слово подходит, суммируем длину
    }

    return result
}