All problems

Problem 194

Decode Ways

Medium · 20 pts · String, Dynamic Programming

Problem

A message of capital letters was encoded as digits using A = 1, B = 2, ..., Z = 26, and the numbers were written next to each other with no separators. Given the digit string, count how many different letter messages could have produced it, modulo 1000000007. Every part must be a number from 1 to 26 written without a leading zero, so "06" is not a valid part and a lone "0" cannot be decoded. Let ways[i] count the decodings of the first i digits: add ways[i - 1] if the last digit is 1-9, and add ways[i - 2] if the last two digits form a number from 10 to 26. Print 0 if the string cannot be decoded. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).

Input

A single string of digits.

Output

Print the number of decodings modulo 1000000007 (0 if none).

Constraints

1 <= length <= 100000

Example input

12

Example output

2

Explanation

"12" can be read as 1 and 2, giving AB, or as the single number 12, giving L. That is 2 decodings.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Partition Equal Subset Sum Next: Min Cost Climbing Stairs