> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Decode Ways Python Solution with Tests

> Tested Python solution for LeetCode 91 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 91, Medium. Topics: String, Dynamic Programming. [View on LeetCode](https://leetcode.com/problems/decode-ways/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 91   # by problem number
lcpy gen -s decode_ways   # by problem name
```

## 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

```
Input: s = "12"
Output: 2
Explanation: "12" could be decoded as "AB" (1 2) or "L" (12).
```

```
Input: s = "226"
Output: 3
Explanation: "226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
```

```
Input: s = "06"
Output: 0
Explanation: leading zero makes it invalid.
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/decode_ways/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/decode_ways/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def num_decodings(self, s: str) -> int:
        if not s:
            return 0

        num_ways_two_steps_behind: int = 1
        num_ways_one_step_behind: int = 0 if s[0] == "0" else 1

        for index in range(1, len(s)):
            current_char: str = s[index]
            previous_char: str = s[index - 1]

            current_num_ways: int = 0

            if current_char != "0":
                current_num_ways += num_ways_one_step_behind

            two_digit_value: int = int(previous_char + current_char)
            if previous_char != "0" and 10 <= two_digit_value <= 26:
                current_num_ways += num_ways_two_steps_behind

            num_ways_two_steps_behind, num_ways_one_step_behind = (
                num_ways_one_step_behind,
                current_num_ways,
            )

        return num_ways_one_step_behind
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(1)  |

## Tags

[Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
