Skip to main content
Back to problems
Leetcode
Medium
Dynamic Programming
Strings
Amazon
Decode Ways

Count how many ways a digit string can be decoded into letters using the mapping 1->A, 2->B, ..., 26->Z.

Acceptance 100%
Problem Statement

You are given a string of digits. Each non-empty segment of 1 or 2 digits can represent a letter if it maps to a value from 1 to 26, where 1 corresponds to A and 26 corresponds to Z.

Return the number of different ways to decode the entire string into letters.

A decode is valid only if every chosen segment is within the range 1 to 26 and segments do not start with 0.

Input Format

  • A single string s consisting only of digits.
  • s represents the encoded message.

Output Format

  • Return an integer: the total number of valid decodings of s.

Constraints

  • 1 <= s.length
  • s contains only characters '0'-'9'
  • A segment may not start with 0.
  • Only values from 1 to 26 are allowed.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "12"

Output

2

Explanation

The string can be decoded as "AB" (1, 2) or "L" (12).

Example 2

Input

s = "226"

Output

3

Explanation

Valid decodings are "BBF" (2,2,6), "BZ" (2,26), and "VF" (22,6).

Show 1 more example

Example 3

Input

s = "06"

Output

0

Explanation

No valid decoding starts with 0.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.