Count Number of Texts - Problem

Alice is texting Bob using her old-school phone keypad. Remember those? Each number key maps to multiple letters, and to type a specific letter, you need to press the key multiple times!

The keypad mapping works like this:

  • Key 2: ABC (press 1×=A, 2×=B, 3×=C)
  • Key 3: DEF (press 1×=D, 2×=E, 3×=F)
  • Key 4: GHI (press 1×=G, 2×=H, 3×=I)
  • Key 5: JKL (press 1×=J, 2×=K, 3×=L)
  • Key 6: MNO (press 1×=M, 2×=N, 3×=O)
  • Key 7: PQRS (press 1×=P, 2×=Q, 3×=R, 4×=S)
  • Key 8: TUV (press 1×=T, 2×=U, 3×=V)
  • Key 9: WXYZ (press 1×=W, 2×=X, 3×=Y, 4×=Z)

For example, to type "bob", Alice would press: 2-22-666-2-22"2266622"

The Problem: Due to transmission errors, Bob only received the sequence of pressed keys (like "2266622") but lost the information about where one letter ends and another begins!

Given a string pressedKeys, return the total number of possible text messages Alice could have sent. Since the answer can be huge, return it modulo 109 + 7.

Input & Output

example_1.py — Basic Case
$ Input: pressedKeys = "22"
Output: 2
💡 Note: Alice could have sent "aa" (press 2 twice) or "b" (press 2 twice). Both interpretations are valid.
example_2.py — Multiple Options
$ Input: pressedKeys = "222"
Output: 3
💡 Note: Three possible messages: "aaa" (2+2+2), "ab" (2+22), or "ba" (22+2). Each grouping represents valid letter combinations.
example_3.py — Complex Sequence
$ Input: pressedKeys = "2266622"
Output: 6
💡 Note: Multiple valid interpretations exist. For example: "bob" (22+666+22), "aoo" (2+666+22), "anm" (2+66+6+22), etc. Each valid parsing contributes to the total count.

Constraints

  • 1 ≤ pressedKeys.length ≤ 105
  • pressedKeys only contains digits [2-9]
  • Each digit corresponds to letters as on traditional phone keypads
  • Keys 7 and 9 have 4 letters each (PQRS, WXYZ)
  • All other keys have 3 letters each

Visualization

Tap to expand
Phone Keypad Text Decoder2ABC3DEF4GHI5JKL6MNO7PQRS8TUV9WXYZExample: Decoding "222"Possible interpretations:• "2" + "2" + "2" → AAA• "2" + "22" → AB• "22" + "2" → BADP Approach:dp[i] = ways to decode string[0:i]For each position i: Try letter lengths 1, 2, 3 (or 4 for 7,9) If valid: dp[i] += dp[i-length]Result: dp[3] = 3 waysRed keys (7,9) have 4 letters!Time: O(n), Space: O(n)
Understanding the Visualization
1
Identify the Pattern
Each sequence of identical digits can form different letters based on length
2
Build Solutions Incrementally
Use DP to count ways: dp[i] = sum of dp[i-len] for all valid letter lengths
3
Handle Special Cases
Keys 7 and 9 have 4 letters (PQRS, WXYZ) while others have 3
4
Optimize with Modular Arithmetic
Keep results manageable with MOD = 10^9 + 7
Key Takeaway
🎯 Key Insight: This is a classic dynamic programming problem where we build up solutions by considering all valid ways to form letters at each position. The constraint that consecutive identical digits form letters makes this similar to the "Decode Ways" problem with additional rules.
Asked in
Meta 25 Google 18 Amazon 15 Microsoft 12
23.4K Views
Medium Frequency
~25 min Avg. Time
892 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