medium1-D Dynamic ProgrammingFacebookAmazonMicrosoft
Numeric Decoder
A spy maps 1→A, 2→B, …, 26→Z and counts the number of distinct letter sequences a digit string could decode to.
Given a numeric string using digits 1-9 and possibly 0, return the number of ways to decode it (A=1,...,Z=26).
Choose approach
1
01
1?
2?
3i
1/3
Narrationstep 1 of 3
Seed dp[0]=dp[1]=1.
Variables
How to think about it
- Each position is reached from previous OR two-back if last two digits form 10..26.
- Leading '0' kills a branch.
Edge cases to catch in interview
- "0" -> 0.
- "06" -> 0 (no leading zero decode).
Examples
Example 1
2{"s":"12"}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed