Skip to content

Practice Decode Ways

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#91
LeetCode Problem

Decode Ways

Problem Statement

Decode a digit string using the integer codes 1 through 26 for letters A through Z. Count all valid ways to split the entire string into such codes. A code cannot start with zero: 0 alone and 06 are invalid, while 10 and 20 are valid. Return zero when no full decoding exists. The answer fits in a signed 32-bit integer. Practice approach: Use dynamic programming over prefixes. A nonzero final digit can extend each decoding of the previous prefix; a final pair from 10 through 26 can extend each decoding from two positions earlier. Add the applicable counts. Keep only the last two counts for O(1) extra space and O(n) time.

Source: Decode Ways on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "12"
Output
2

# Example 2

Input
s = "226"
Output
3

# Example 3

Input
s = "06"
Output
0

Constraints

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

Related LeetCode Problems

Practice more problems similar to Decode Ways.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.