VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 12: Dynamic programming

Lesson 12.10 of 16 in this module Unrolled Pro

Decode Ways

Count the ways a digit string decodes into letters with A = 1, …, Z = 26. From trying every split into one- and two-digit pieces (exponential), to a DP over prefixes where each position adds the ways from one and two characters back when that piece is valid (O(n), O(1) space), with the zeros that make it tricky. Then the '*' wildcard, the counts modulo a prime, listing the decodings, and the ML twin: counting (and scoring) the ways an ambiguous token stream can be read.

about 10 min 3 Python examples 2 quick checks

Problem. A message of letters A–Z was encoded as digits with A → 1, B → 2, …, Z → 26. Given the digit string, return the number of ways to decode it.

text
"12"     →   2      ("AB" from 1 2, "L" from 12)
"226"    →   3      (2 2 6, 22 6, 2 26)
"06"     →   0      ("06" isn't a valid code, and 0 alone maps to nothing)
"10"     →   1      (only 10 = J)

It's lesson 12.2's climbing stairs with conditions on the steps: from position i you may take one digit if it's 1–9, or two digits if they form 10–26. The zeros are where solutions go wrong: a 0 can never stand alone, so it must be the second digit of 10 or 20.

ML context. Counting the readings of an ambiguous sequence is everywhere in sequence modelling: the number of ways an OCR or speech output can be split into symbols, the readings of a byte stream as multi-byte characters, alignments in a segmentation lattice. When each reading has a probability, the same DP sums them (the forward algorithm) or takes the best one (Viterbi, lesson 12.16); Change D shows the sum.


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Decode Ways"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee