Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2266.html

2266. Count Number of Texts

  • Difficulty: Medium.
  • Related Topics: Hash Table, Math, String, Dynamic Programming.
  • Similar Questions: Letter Combinations of a Phone Number, Decode Ways.

Problem

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.

  • For example, to add the letter 's', Alice has to press '7' four times. Similarly, to add the letter 'k', Alice has to press '5' twice.

  • Note that the digits '0' and '1' do not map to any letters, so Alice does not use them.

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

  • For example, when Alice sent the message "bob", Bob received the string "2266622".

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:

  • 1 <= pressedKeys.length <= 105

  • pressedKeys only consists of digits from '2' - '9'.

Solution (Java, C++, Python)

  • class Solution {
        public int countTexts(String pressedKeys) {
            int len = pressedKeys.length();
            long dp0 = 1L;
            long dp1 = 0;
            long dp2 = 0;
            long dp3 = 0;
            long dp4;
            char[] keys = pressedKeys.toCharArray();
            int base = 1000000007;
            for (int i = 1; i < len; i++) {
                int r = keys[i] - '0';
                dp4 = dp3;
                dp3 = dp2;
                dp2 = dp1;
                dp1 = dp0 % base;
                dp0 = dp1;
                dp0 += (i - 1 == 0 && keys[i] == keys[i - 1]) ? 1 : 0;
                if (i - 1 <= 0 || keys[i] != keys[i - 1]) {
                    continue;
                }
                dp0 += dp2;
                dp0 += (i - 2 == 0 && keys[i] == keys[i - 2]) ? 1 : 0;
                if (i - 2 <= 0 || keys[i] != keys[i - 2]) {
                    continue;
                }
                dp0 += dp3;
                dp0 += (i - 3 == 0 && keys[i] == keys[i - 3] && (r == 7 || r == 9)) ? 1 : 0;
                if (i - 3 <= 0 || keys[i] != keys[i - 3] || (r != 7 && r != 9)) {
                    continue;
                }
                dp0 += dp4;
            }
    
            return (int) (dp0 % base);
        }
    }
    
    ############
    
    class Solution {
        private static final int N = 100010;
        private static final int MOD = (int) 1e9 + 7;
        private static long[] f = new long[N];
        private static long[] g = new long[N];
        static {
            f[0] = 1;
            f[1] = 1;
            f[2] = 2;
            f[3] = 4;
            g[0] = 1;
            g[1] = 1;
            g[2] = 2;
            g[3] = 4;
            for (int i = 4; i < N; ++i) {
                f[i] = (f[i - 1] + f[i - 2] + f[i - 3]) % MOD;
                g[i] = (g[i - 1] + g[i - 2] + g[i - 3] + g[i - 4]) % MOD;
            }
        }
    
        public int countTexts(String pressedKeys) {
            long ans = 1;
            for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
                int j = i;
                char c = pressedKeys.charAt(i);
                for (; j + 1 < n && pressedKeys.charAt(j + 1) == c; ++j)
                    ;
                int cnt = j - i + 1;
                ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
                ans %= MOD;
                i = j;
            }
            return (int) ans;
        }
    }
    
  • mod = 10**9 + 7
    f = [1, 1, 2, 4]
    g = [1, 1, 2, 4]
    for _ in range(100000):
        f.append((f[-1] + f[-2] + f[-3]) % mod)
        g.append((g[-1] + g[-2] + g[-3] + g[-4]) % mod)
    
    
    class Solution:
        def countTexts(self, pressedKeys: str) -> int:
            ans = 1
            for ch, s in groupby(pressedKeys):
                m = len(list(s))
                ans = ans * (g[m] if ch in "79" else f[m]) % mod
            return ans
    
    ############
    
    # 2266. Count Number of Texts
    # https://leetcode.com/problems/count-number-of-texts/
    
    class Solution:
        def countTexts(self, A: str) -> int:
            n = len(A)
            M = 10 ** 9 + 7
            
            @cache
            def go(i):
                if i == n: return 1
                
                res = go(i + 1)
                
                if A[i] in "79":
                    for k in range(i + 1, min(n, i + 4)):
                        if A[k] == A[i]:
                            res += go(k + 1)
                        else:
                            break
                else:
                    for k in range(i + 1, min(n, i + 3)):
                        if A[k] == A[i]:
                            res += go(k + 1)
                        else:
                            break
                    
                return res % M
            
            return go(0)
                    
                
                
    
    
  • const mod int = 1e9 + 7
    const n int = 1e5 + 10
    
    var f = [n]int{1, 1, 2, 4}
    var g = f
    
    func init() {
    	for i := 4; i < n; i++ {
    		f[i] = (f[i-1] + f[i-2] + f[i-3]) % mod
    		g[i] = (g[i-1] + g[i-2] + g[i-3] + g[i-4]) % mod
    	}
    }
    
    func countTexts(pressedKeys string) int {
    	ans := 1
    	for i, j, n := 0, 0, len(pressedKeys); i < n; i++ {
    		c := pressedKeys[i]
    		j = i
    		for j+1 < n && pressedKeys[j+1] == c {
    			j++
    		}
    		cnt := j - i + 1
    		if c == '7' || c == '9' {
    			ans = ans * g[cnt] % mod
    		} else {
    			ans = ans * f[cnt] % mod
    		}
    		i = j
    	}
    	return ans
    }
    

Explain:

nope.

Complexity:

  • Time complexity : O(n).
  • Space complexity : O(n).

All Problems

All Solutions