Show problems as
Classic problem #91MediumStringDynamic Programming

Decode Ways

Real-world version: Split The Swatch Tape
Tracked in this browser only
Read Decode Ways on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A colour lab keeps twenty-six swatches, numbered 1 through 26. When a batch is mixed, the machine punches the swatch numbers onto a paper tape one after another with no separator between them, so a batch of swatch 8 then swatch 15 comes out as the tape "815".

You are handed a finished tape as the digit string tape and have to work out how many batches could have punched it. A swatch number is punched with no padding, so no number on the tape may begin with 0, and every number must land in the range 1 to 26.

Return the count of batches that produce tape. Return 0 when the tape cannot have come from any batch.

Examples

Example 1

Input
tape = "2613"
Output
4

The tape splits as 2,6,1,3 or 26,1,3 or 2,6,13 or 26,13, so four batches fit.

Example 2

Input
tape = "907"
Output
0

The middle 0 cannot stand alone and 90 is past 26, so nothing accounts for it.

Example 3

Input
tape = "1120"
Output
2

The trailing 0 has to join the 2 in front of it as 20, leaving 1,1,20 or 11,20.

Constraints

  • 1 <= tape.length <= 100
  • tape contains only digits and may begin with one or more zeros.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def count_tape_readings(tape: str) -> int:
Java
public int countTapeReadings(String tape)