Skip to main content
LeetCode 91, Medium. Topics: String, Dynamic Programming. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 13 parametrized pytest cases, and a playground notebook:

Problem

You have intercepted a secret message encoded as a string of numbers. The message is decoded via the mapping: "1" -> 'A', "2" -> 'B', ..., "26" -> 'Z'. Given a string s containing only digits, return the number of ways to decode it. Return 0 if it cannot be decoded.

Examples

Constraints

  • 1 <= s.length <= 100
  • s contains only digits and may contain leading zero(s)

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Grind, Blind 75, NeetCode 150, NeetCode 250, NeetCode All.
Last modified on August 25, 2026