Decode Ways II

A message containing letters from A-Z can be encoded into numbers using the following mapping:

  • 'A' -> "1"
  • 'B' -> "2"
  • ...
  • 'Z' -> "26"

To decode an encoded message, all the digits must be grouped and then mapped back into letters using the reverse of the mapping above. There may be multiple ways to decode a message.

For example, the grouping (1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06".

In addition to the mapping above, an encoded message may contain the '*' character, which can represent any digit from '1' to '9'; '0' is excluded. Decoding a string containing '*' is equivalent to decoding any of the encoded messages it can represent.

Given a string s consisting of digits and '*' characters, return the number of ways to decode it.

Since the answer may be very large, return it modulo 10^9 + 7.

Example 1
Inputs = "*"
Output9
The encoded message can represent any of the encoded messages "1" through "9", each of which decodes to one letter, so there are 9 ways.
Example 2
Inputs = "1*"
Output18
The encoded message can represent "11" through "19", and each of these encoded messages has 2 ways to be decoded, so there are 18 ways.

Constraints

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

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate