One- or two-digit prefix DP
Read a Numbered Beacon Message
01. The question
Beacon letters use numbers 1 through 26. Count the ways to split a digit message into valid letter numbers. A lone zero and a leading-zero number are invalid. Original beacon scenario; an empty received message returns 0.
Before revealing the idea, predict the result and describe what information you would need to track.
02. Worked example
Input"1210"
Output2
- "12" can be 1|2 or 12, giving two ways.
- At prefix "121" a final 1 or 21 gives three ways.
- The final zero cannot stand alone; only a last token 10 works, leaving 1|2|10 and 12|10.
Try your code
Define solve(data, parameter="") and return your result. Your code runs locally, not on a server.
Edit your test cases
Use 1-10 objects with data and an optional string parameter. Expected outputs are computed from the lesson. An optional expected field must be correct.
Input contract & reference code
Example data: "1210". Parameter: "". Numeric parameters arrive as strings; convert them with int(parameter) when needed.
The example and generated cases use the question’s exact variant. This checks behavior on a small test batch—not performance on every possible input.
Python downloads only when you run code, using compressed runtime assets. Code is not saved or uploaded. Enable JavaScript to run it.
Printed output
Five-second execution limit per batch. Standard-library Python only; additional packages and network access are unavailable. Stop ends execution without blocking the lesson.
03. The idea
Try sketching your own approach first. What would you track, and why would it work?
Reveal the idea
- Seed the empty internal prefix with one way.
- Add the previous prefix when the last digit is not zero.
- Add the two-back prefix only for a last pair from 10 to 26.
04. Watch it happen
Use Step to move slowly, or Play to follow the whole trace. Change a preset or load your own input.
Input & settings
Red = current item / blue = active window or cells / green = completed work. A value docks at its actual destination; the original input stays unchanged. Positions start at 0.
Working state / what changes
Ready to begin
Enable JavaScript to step through the animation. The worked example and solution below remain readable without it.
Python / why it changes
The line behind the step
def solve(data, parameter=''):
if not data:
return 0
ways = [0] * (len(data) + 1)
ways[0] = 1
for end in range(1, len(data) + 1):
if data[end - 1] != '0':
ways[end] += ways[end - 1]
if end >= 2 and 10 <= int(data[end - 2:end]) <= 26:
ways[end] += ways[end - 2]
return ways[-1]How to use this walkthrough
Hover or focus a control for its label. Play forms the input and follows values into the working state. Step shows one change; Back and the slider restore exact snapshots. Reduced motion uses instant updates. Saved snapshots and sorted heap displays are teaching overhead, not algorithm complexity.
05. Python solution
def solve(data, parameter=''):
if not data:
return 0
ways = [0] * (len(data) + 1)
ways[0] = 1
for end in range(1, len(data) + 1):
if data[end - 1] != '0':
ways[end] += ways[end - 1]
if end >= 2 and 10 <= int(data[end - 2:end]) <= 26:
ways[end] += ways[end - 2]
return ways[-1]06. Cost & edge cases
- Time
- O(n)
- Space
- O(n)
- "" and "0" return 0; "10" returns 1.
- "06" is invalid, not a code for 6.
- Use a JSON string containing 0-14 decimal digits; never a JSON number that loses leading zeros.