InferenceAI

Enter at least two characters to search.

Start free

Decode a Digit Message

mediumDynamic Programming25 min25 XP

Letters are encoded as A = "1", ..., Z = "26". Return the number of ways to decode the digit string s. Parts can't have leading zeros, so "06" is not valid.

Examples

EXAMPLE WALKTHROUGHtwo ways

A guided view of the published example, not a trace of your code.

INPUT["12"]
TASKdecode_digit_message()
EXPECTED OUTPUT2

Start with the input values shown in this example.

Example 1 of 3

Input: s = "12"

Output: 2

Explanation: two ways

Input: s = "226"

Output: 3

Explanation: three ways

Input: s = "06"

Output: 0

Explanation: leading zero

Hint 1

ways[i] = ways[i - 1] if s[i - 1] != '0', plus ways[i - 2] if s[i-2:i] is between 10 and 26.

Topics

DSADynamic ProgrammingPython