Ctrl K

Ways to Read a Digit Code

Medium
Kids encode their notes by replacing each capital English letter with its position in the alphabet, A → 1, B → 2, …, Z → 26, and writing the numbers back to back with no separators. That makes many digit strings ambiguous: 25 could be Y or BE. Given a digit string code, count the ways to split it into letter codes. A code cannot start with zero: 06 is not F. If the string cannot be read at all, return 0.
Example 1
code='2611' → 4

Four readings: 2 6 1 1 (BFAA), 26 1 1 (ZAA), 2 6 11 (BFK), 26 11 (ZK).

Example 2
code='1203' → 1

A zero can only be the second digit of 10 or 20, so the only reading is 1 20 3 (ATC).

Example 3
code='30' → 0

30 is not a letter code and 0 alone means nothing, so there are no readings.

Constraints
  • 1 ≤ len(code) ≤ 100
  • code consists of digits only and may start with a zero
  • The answer is less than 2³¹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Mirror Fragment MediumAll Balanced Bracket Strings MediumFewest Letter Edits MediumWeaving Two Strings MediumCutting a Word Into Palindromes Medium
loading Python…
Input
code='2611'
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 143 tests on the server, hidden ones included.