Tribonacci and Tetranacci precomputed numbers solution
We can break down our full array into sub-arrays of repeating numbers.\nWe will find the answer for every one of those sub-arrays and then multiply them.\n\nHan
We can break down our full array into sub-arrays of repeating numbers.\nWe will find the answer for every one of those sub-arrays and then multiply them.\n\nHandling a subarray, we have two possibilties:\n* numbers with 3 possible letter combinations (1,2,3,4,5,6,8) \n* numbers with 4 possible letter combinations (7, 9)\n\nIn the first case one digit can participate in an encoding with 3 possible lengths (1,2,3)\nIn the second case one digit can participate in an encoding with 4 possible lengths (1,2,3,4)\ntherefore the recursive relationships are:\n\nfirst: f[n] = f[n-1] + f[n-2] + f[n-3]\nand\nsecond: f[n] = f[n-1] + f[n-2] + f[n-3] + f[n-4]\n\nYou can calculate using your fingers and furrowing your eyebrows the base cases for these sequences. \nWe can precompute them.\n\n```\nclass Solution {\n    val INF = 1000000007\n    val MM = 100001\n    val dp3 = LongArray(MM)\n    val dp4 = LongArray(MM)\n    fun countTexts(s: String): Int {\n        //tribonacci\n        dp3[1] = 1\n        dp3[2] = 2\n        dp3[3] = 4\n        for (i in 4 until MM) dp3[i] = (dp3[i-1] + dp3[i-2] + dp3[i-3]) % INF\n        //tetranacci\n        dp4[1] = 1\n        dp4[2] = 2\n        dp4[3] = 4\n        dp4[4] = 8\n        for (i in 5 until MM) dp4[i] = (dp4[i-1] + dp4[i-2] + dp4[i-3] + dp4[i-4]) % INF\n        var count = 1L        \n        var i = 0\n        val n = s.length\n        while (i < n) {\n            var len = 1\n            while (i + 1 < n && s[i+1] == s[i]) {\n                i++\n                len++\n            }\n            if (s[i] == \'7\' || s[i] == \'9\') count *= dp4[len]\n            else count *= dp3[len]      \n            count %= INF\n            i++\n        }\n        return count.toInt()\n    }\n}\n```
['Kotlin']