Problem 599016 · medium · Phase 05 Advanced Algorithms & Graphs

Digit Cipher

dynamic programming · 1-D dp · strings · counting · modular arithmetic

A cipher replaces each letter with its position in the alphabet: A -> 1, B -> 2, ..., Z -> 26, and the numbers are written back to back with no separators. Given the resulting digit string code, return how many different letter strings could have produced it, modulo 10**9 + 7. A code with no valid reading has 0 readings.

Examples

Input:  code = "1226"
Output: 5
Explanation: ABBF, LBF, AVF, ABZ, LZ.

Input:  code = "06"
Output: 0
Explanation: a leading zero is never a letter.

Constraints

  • 1 <= len(code) <= 10**5, digits only
  • Target complexity: O(n) time.

Goals

  • Count parses of a string by looking at the last one or two characters
  • Handle zeros, which can only be the second digit of a pair
Starting Python…