Decode Ways II

HardDynamic Programming1d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Letters are encoded as numbers: 'A' is 1, 'B' is 2, and so on up to 'Z' = 26. The string s is an encoded message made of digits and the wildcard '*'. Each '*' can stand for any digit from 1 to 9 (never 0). Count the ways to split s into codes from 1 to 26, counting every choice of wildcard digits. A code never has a leading zero: "06" is not a valid code, and a lone "0" cannot be decoded. Return the count modulo 10^9 + 7.

Examples

Input: s = "*"
Output: 9
The wildcard can be 1 to 9, and each is one letter.
Input: s = "1*"
Output: 18
For each of the 9 digits d, "1d" decodes as two letters or as the single code 1d, giving 9 * 2 = 18.
Input: s = "2*"
Output: 15
All 9 choices decode as two letters. Only 21 to 26 also work as a single code, adding 6, for 15 total.

Constraints

  • 1 <= s.length <= 10^5
  • `s[i]` is a digit or `'*'`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 1d dp problems