Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-063

Decode Ways

Problem

Count the number of ways to decode a digit string s where 1 maps to A through 26 mapping to Z.

Use dynamic programming: a single digit can stand alone when it is not zero, and a two-digit group is valid only when it lies between 10 and 26. Return 0 for the empty string or any string that cannot be fully decoded.

Hint

A leading zero can only be consumed as the second digit of 10 or 20.

Test cases

case 1
in: ["12"]
expected: 2
case 2
in: ["226"]
expected: 3
case 3
in: ["06"]
expected: 0
case 4
in: ["10"]
expected: 1
case 5
in: ["27"]
expected: 1
case 6
in: [""]
expected: 0
Starter codePython
def num_decodings(s):
    # Your code here
    pass

6 test cases · run your code against them in the editor