Count Number of Texts - Problem

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

Digit to Letter Mapping:

  • 2: abc
  • 3: def
  • 4: ghi
  • 5: jkl
  • 6: mno
  • 7: pqrs
  • 8: tuv
  • 9: wxyz

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: 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.

Input & Output

Example 1 — Basic Case
$ Input: pressedKeys = "22"
Output: 2
💡 Note: Two ways to decode "22": 1) Press '2' once for 'a', then '2' once for 'a' → "aa", 2) Press '2' twice for 'b' → "b"
Example 2 — Multiple Digits
$ Input: pressedKeys = "222"
Output: 3
💡 Note: Three ways: 1) "aaa" (2+2+2), 2) "ab" (2+22), 3) "ba" (22+2). Cannot use 222 as '2' key has only 3 letters (abc)
Example 3 — Different Digits
$ Input: pressedKeys = "23"
Output: 1
💡 Note: Only one way: 1) "ad" (2+3). Cannot combine different digits or split differently.

Constraints

  • 1 ≤ pressedKeys.length ≤ 105
  • pressedKeys consists of digits ["2", "9"]
  • pressedKeys only contains valid phone keypad digits

Visualization

Tap to expand
Count Number of Texts INPUT 2 abc 3 def 4 ghi 5 jkl 6 mno 7 pqrs Pressed Keys: 2 2 pressedKeys = "22" Key 2: press 1x=a, 2x=b, 3x=c Keys 7,9 have 4 letters Others have 3 letters Return count mod 10^9+7 ALGORITHM STEPS 1 Initialize DP Array dp[i] = ways to decode first i characters 2 Group Same Digits "22" = group of 2 same digits (both are '2') 3 Apply DP Transition dp[i] += dp[i-1] dp[i] += dp[i-2] if same 4 Count Possibilities "2"+"2" = "a"+"a" = "aa" "22" = "b" (press 2 twice) DP Table i: 0 1 2 dp: 1 1 2 FINAL RESULT Possible Messages: Option 1: "aa" "2" + "2" = 'a' + 'a' Option 2: "b" "22" = 'b' (pressed 2x) OUTPUT 2 OK - 2 ways! Total possible messages Alice could have sent Key Insight: This is a Dynamic Programming problem similar to "Climbing Stairs". For consecutive same digits, we can group 1-3 presses (or 1-4 for keys 7,9). dp[i] = dp[i-1] + dp[i-2] + dp[i-3] for same digits. Time: O(n), Space: O(n) or O(1) with optimization. Use modulo 10^9+7 for large results. TutorialsPoint - Count Number of Texts | Optimal DP Solution
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
85.0K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen