Count Number of Texts

medium dp string

Problem

On an old phone keypad each digit maps to letters: 2 to 6 and 8 give 3 letters each, while 7 and 9 give 4 letters. To type a letter you press its digit once for the first letter, twice for the second, and so on. Given a string pressedKeys of the keys Alice actually pressed, count how many distinct text messages those presses could spell out. Return the count modulo 1000000007.

InputpressedKeys = "22233"
Output8
The run "222" can be read 4 ways (aaa, ab, ba, c) and "33" can be read 2 ways (dd, e), so 4 × 2 = 8.

def count_texts(pressed_keys):
    MOD = 10**9 + 7
    s = pressed_keys
    total, i, n = 1, 0, len(s)
    while i < n:
        j = i
        while j < n and s[j] == s[i]:
            j += 1
        run = j - i
        k = 4 if s[i] in "79" else 3
        dp = [1] + [0] * run
        for x in range(1, run + 1):
            for t in range(1, k + 1):
                if x - t >= 0:
                    dp[x] = (dp[x] + dp[x - t]) % MOD
        total = (total * dp[run]) % MOD
        i = j
    return total
function countTexts(pressedKeys) {
  const MOD = 1000000007n;
  const s = pressedKeys, n = s.length;
  let total = 1n, i = 0;
  while (i < n) {
    let j = i;
    while (j < n && s[j] === s[i]) j++;
    const run = j - i;
    const k = (s[i] === "7" || s[i] === "9") ? 4 : 3;
    const dp = new Array(run + 1).fill(0n);
    dp[0] = 1n;
    for (let x = 1; x <= run; x++) {
      for (let t = 1; t <= k; t++) {
        if (x - t >= 0) dp[x] = (dp[x] + dp[x - t]) % MOD;
      }
    }
    total = (total * dp[run]) % MOD;
    i = j;
  }
  return Number(total);
}
class Solution {
    public int countTexts(String pressedKeys) {
        long MOD = 1000000007L;
        String s = pressedKeys;
        int n = s.length(), i = 0;
        long total = 1;
        while (i < n) {
            int j = i;
            while (j < n && s.charAt(j) == s.charAt(i)) j++;
            int run = j - i;
            int k = (s.charAt(i) == '7' || s.charAt(i) == '9') ? 4 : 3;
            long[] dp = new long[run + 1];
            dp[0] = 1;
            for (int x = 1; x <= run; x++) {
                for (int t = 1; t <= k; t++) {
                    if (x - t >= 0) dp[x] = (dp[x] + dp[x - t]) % MOD;
                }
            }
            total = (total * dp[run]) % MOD;
            i = j;
        }
        return (int) total;
    }
}
int countTexts(string pressedKeys) {
    const long long MOD = 1000000007LL;
    string s = pressedKeys;
    int n = s.size(), i = 0;
    long long total = 1;
    while (i < n) {
        int j = i;
        while (j < n && s[j] == s[i]) j++;
        int run = j - i;
        int k = (s[i] == '7' || s[i] == '9') ? 4 : 3;
        vector<long long> dp(run + 1, 0);
        dp[0] = 1;
        for (int x = 1; x <= run; x++) {
            for (int t = 1; t <= k; t++) {
                if (x - t >= 0) dp[x] = (dp[x] + dp[x - t]) % MOD;
            }
        }
        total = (total * dp[run]) % MOD;
        i = j;
    }
    return (int) total;
}
Time: O(n) Space: O(n)