Skip to content
BytePatterns

Decode Digit Message

MediumDynamic Programming#bottom-up-dp#rolling-variables~30m

Problem

Letters were turned into numbers, A to Z becoming 1 to 26, and the numbers were then written down with no separators. Count how many different letter sequences could have produced a given digit text. A group of digits is only a letter when it reads as 1 through 26, so no group may start with a zero.

Examples

Input:  digits = "226"
Output: 3
Why:    the cuts 2 26, 22 6 and 2 2 6 are all legal
Input:  digits = "11106"
Output: 2
Why:    the zero must join the digit before it, which rules out most cuts
Input:  digits = "0"
Output: 0
Why:    edge case, nothing decodes a leading zero

Hints

0 / 3

Stuck on the idea rather than the code? Top-Down vs Bottom-Up covers it.