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: 2
💡 Note: Two ways: 1) "ad" (2+3), 2) "ae" (2+3). Note: cannot combine different digits like "23"

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: "22" ExampleInput: "22"Bob received these keysWay 1:Split: 2 + 2Letters: a + aWay 2:Split: 22Letters: bInvalid:Cannot use 222Key 2 has only abcPhone Key 2:1 press = a2 press = b3 press = cOutput: 2Two possible messages: "aa" or "b"
Understanding the Visualization
1
Input
Pressed keys string from transmission
2
Process
Find all valid ways to group consecutive same digits
3
Output
Total count of possible original messages
Key Takeaway
🎯 Key Insight: Count valid ways to split consecutive same digits based on phone key letter counts
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