<commit_before>package leetcode

/**
 * https://leetcode.com/problems/count-number-of-texts/
 */
class Problem2266 {
    fun countTexts(pressedKeys: String): Int {
        TODO()
    }
}

fun main() {
    val prob = Problem2266()
    println(prob.countTexts("22233")) // 8
    println(prob.countTexts("222")) // 4
    println(prob.countTexts("2222")) // 7
    println(prob.countTexts("22222")) // 13
    println(prob.countTexts("222222")) // 24
    println(prob.countTexts("2222222")) // 44
    println(prob.countTexts("222222222222222222222222222222222222")) // 82876089
}<commit_msg>Solve problem 2266<commit_after>package leetcode

/**
 * https://leetcode.com/problems/count-number-of-texts/
 */
class Problem2266 {
    fun countTexts(pressedKeys: String): Int {
        if (pressedKeys.length == 1) {
            return 1
        }
        var answer = 1L
        var i = 0
        var length = 1
        val memo3 = IntArray(pressedKeys.length + 1) { -1 }
        val memo4 = IntArray(pressedKeys.length + 1) { -1 }
        while (i < pressedKeys.length - 1) {
            if (pressedKeys[i] != pressedKeys[i + 1]) {
                val numDigits = numDigits(pressedKeys[i])
                answer = (answer * countTexts(
                    numDigits,
                    length,
                    if (numDigits == 3) memo3 else memo4
                )) % 1_000_000_007
                length = 1
            } else {
                length++
            }
            i++
        }
        val numDigits = numDigits(pressedKeys[i])
        answer = (answer * countTexts(
            numDigits,
            length,
            if (numDigits == 3) memo3 else memo4
        )) % 1_000_000_007
        return answer.toInt()
    }

    private fun numDigits(key: Char): Int {
        return if (key == '7' || key == '9') 4 else 3
    }

    private fun countTexts(numDigits: Int, length: Int, memo: IntArray): Int {
        if (length < 0) {
            return 0
        }
        if (length == 0) {
            return 1
        }
        if (memo[length] != -1) {
            return memo[length]
        }
        var count = 0
        for (digit in 1..numDigits) {
            count = (count + countTexts(numDigits, length - digit, memo)) % 1_000_000_007
        }
        memo[length] = count
        return count
    }
}
{'fraction_non_alphanumeric': 0.12285223367697594, 'fraction_numerical': 0.06872852233676977, 'mean_word_length': 1.6800920598388953, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '237206', 'n_tokens_mistral': 849, 'n_tokens_neox': 717, 'n_words': 165}