LeetCode in Kotlin

2266. Count Number of Texts

Medium

Alice is texting Bob using her phone. The mapping of digits to letters is shown in the figure below.

In order to add a letter, Alice has to press the key of the corresponding digit i times, where i is the position of the letter in the key.

However, due to an error in transmission, Bob did not receive Alice’s text message but received a string of pressed keys instead.

Given a string pressedKeys representing the string received by Bob, return the total number of possible text messages Alice could have sent.

Since the answer may be very large, return it modulo 109 + 7.

Example 1:

Input: pressedKeys = “22233”

Output: 8

Explanation:

The possible text messages Alice could have sent are:

“aaadd”, “abdd”, “badd”, “cdd”, “aaae”, “abe”, “bae”, and “ce”.

Since there are 8 possible messages, we return 8.

Example 2:

Input: pressedKeys = “222222222222222222222222222222222222”

Output: 82876089

Explanation: There are 2082876103 possible text messages Alice could have sent.

Since we need to return the answer modulo 109 + 7, we return 2082876103 % (109 + 7) = 82876089.

Constraints:

Solution

class Solution {
    fun countTexts(pressedKeys: String): Int {
        val len = pressedKeys.length
        var dp0 = 1L
        var dp1: Long = 0
        var dp2: Long = 0
        var dp3: Long = 0
        var dp4: Long
        val keys = pressedKeys.toCharArray()
        val base = 1000000007
        for (i in 1 until len) {
            val r = keys[i].code - '0'.code
            dp4 = dp3
            dp3 = dp2
            dp2 = dp1
            dp1 = dp0 % base
            dp0 = dp1
            dp0 += (if (i - 1 == 0 && keys[i] == keys[i - 1]) 1 else 0).toLong()
            if (i - 1 <= 0 || keys[i] != keys[i - 1]) {
                continue
            }
            dp0 += dp2
            dp0 += (if (i - 2 == 0 && keys[i] == keys[i - 2]) 1 else 0).toLong()
            if (i - 2 <= 0 || keys[i] != keys[i - 2]) {
                continue
            }
            dp0 += dp3
            dp0 += (if (i - 3 == 0 && keys[i] == keys[i - 3] && (r == 7 || r == 9)) 1 else 0).toLong()
            if (i - 3 <= 0 || keys[i] != keys[i - 3] || r != 7 && r != 9) {
                continue
            }
            dp0 += dp4
        }
        return (dp0 % base).toInt()
    }
}