Count possible decoding of a given digit sequence
Detailed guide and Python implementation for the 'Count possible decoding of a given digit sequence' problem.
Problem Statement
Write a function count_decodings(digits) that takes a string of digits and returns the number of possible ways to decode it, where 'A' = 1, 'B' = 2, ..., 'Z' = 26.
For example, '12' can be decoded as 'AB' (1, 2) or 'L' (12), giving 2 ways.
If the string contains '0' in an invalid position (e.g., leading '0' or '30'), those paths are invalid and should not be counted.
- •1 <= len(digits) <= 20
- •digits contains only characters '0' through '9'
Examples
count_decodings('12')2
'12' can be decoded as 'AB' (1,2) or 'L' (12). So 2 ways.
count_decodings('226')3
'226' can be decoded as 'BBF' (2,2,6), 'BZ' (2,26), or 'VF' (22,6). So 3 ways.
count_decodings('06')0
'06' cannot be decoded because '0' has no letter mapping and '06' is not a valid code.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Variables & Data Types Explained
Understand Python variables and core data types (strings, integers, floats, booleans). A complete beginner guide to memory assignment in Python.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python Dictionary Methods
Learn Python dictionary methods. Complete reference guide for key-value pair insertions, retrievals, updates, and checks.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.