Ctrl K

Letter Codes With Wildcards

Hard
Letters are encoded as numbers: A is 1, B is 2, …, Z is 26. A message was written as one unbroken string of digits, so it can often be read in several ways: 126, for instance, is ABF, LF or AZ. A number with a leading zero (05) is not a letter, and neither is a lone 0. Some digits have worn off and show as *; each one could be any digit from 1 to 9 (never 0). Different choices for the stars give different messages, and all their readings count. Given code, a string of digits and stars, return the total number of ways to read it as text. The answer can be huge, so return it modulo 10⁹ + 7.
Example 1
code="3*" → 9

* becomes any digit from 1 to 9. Each variant can be read digit by digit (9 ways), and none of 31…39 is a letter, since they exceed 26. Total: 9.

Example 2
code="*0" → 2

A zero is not a letter on its own, so it must pair with the digit before it: only 10 and 20 work. The answer is 2.

Example 3
code="12" → 2

1 2 reads as AB and 12 reads as L: two readings.

Constraints
  • 1 ≤ len(code) ≤ 10⁵
  • code consists of digits and * characters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPattern With Dot and Star HardLongest Balanced Bracket Stretch HardDoes the Text Fit the Pattern HardShuffle by Cutting and Swapping HardWays to Cross Out Hard
loading Python…
Input
code="3*"
Expected
9

Run checks the first 3 tests in your browser. Submit checks all 160 tests on the server, hidden ones included.