Skip to content
AI360Xpert
Beta
Difficulty: Medium1-D Dynamic Programming

Decode Ways

Problem in Plain English

Count how many ways the digit string s splits into pieces from 1 to 26 with no leading zeros, where each piece stands for one letter.

Problem Statement

Letters A through Z correspond to decimal codes 1 through 26. Given a nonempty string of digits, count the ways to divide the entire string into valid letter codes. A piece may contain one nonzero digit, or two digits forming a number from 10 through 26.

Zero cannot stand alone and leading zeros in a piece are forbidden: 06 is invalid, while 10 and 20 are valid. Return zero if no complete decoding exists. The final answer is guaranteed to fit a signed 32-bit integer.

Constraints

  • The string length is between 1 and 100.
  • The string contains decimal digits only and may begin with zero.
  • The final decoding count fits a signed 32-bit integer. Intermediate prefix counts need not fit that range.

Examples

Example 1

Input
s = "12"
Output
2

The split [1,2] represents AB, and [12] represents L. These are the only possible first-piece lengths, and both finish successfully.

Example 2

Input
s = "226"
Output
3

The complete splits are [2,2,6] for BBF, [22,6] for VF, and [2,26] for BZ. The number 226 is outside the code range.

Example 3

Input
s = "06"
Output
0

Zero is not a standalone letter code, and 06 has a leading zero. Neither possible first piece is legal.

Example 4

Input
s = "11106"
Output
2

The splits [1,1,10,6] and [11,10,6] are valid. Splits using 0 alone or the piece 06 are invalid, so the zero removes other apparent choices.

Example 5

Input
s = "10"
Output
1

Only the pair 10 represents a letter, J. The single-digit choice 1 leaves an undecodable zero.

Intuition

Each decoding chooses whether the next letter consumes one digit or two. In "12", taking "1" leaves "2" and gives AB, while taking "12" gives L. Those are two distinct complete splits. At any position a zero blocks the single-digit choice, and a two-digit choice is allowed only for 10 through 26. Many paths reach the same suffix, so remembering counts removes repeated search; counting prefixes needs only the previous two counts.

Approaches

Recursive Branching

Solution Details

Reveal Recursive Branching: intuition, complexity, and code

Hints

Hint 1
At each position, decide whether the next code consumes one digit or two; test legality before following a branch.
Hint 2
For prefixes, separate decodings by whether their final code has length one or two.
Hint 3
Keep counts for the previous two prefix lengths, and use exact arithmetic until the final answer is known.

Edge Cases

  • A single nonzero digit has one decoding; the single digit zero has none.
  • A leading zero blocks the whole message, as in 06, even though 6 alone is valid.
  • 10 and 20 each have one decoding; 30 and 00 have none, because zero cannot stand alone.
  • Pair 26 is valid, but pair 27 is not: 26 has two decodings whereas 27 has one.
  • For 100, pair 10 leaves an invalid final zero, so the answer is zero.
  • A prefix of 98 ones has a very large count; appending 30 makes the length-100 full string undecodable. Final-result bounds do not bound arbitrary prefix counts.

Common Mistakes and Interview Tips

  • Counting an empty suffix as zero loses every completed split; it contributes exactly one successful completion.
  • Parsing a pair without rejecting leading zero accepts 06 incorrectly. Character checks for first digit 1 or 2 avoid this.
  • Rejecting every zero loses legal 10 and 20 pairs; disable only the single-digit contribution.
  • Updating prev2 or prev1 before computing both contributions reads a count for the wrong prefix length.
  • Using Java int or JavaScript Number for every prefix assumes a bound the statement gives only for the final answer.
  • Interview tip: explain why the two last-code groups are disjoint, then distinguish peak live counters from the size of the integers they hold.
  • Plain recursion can have exponential work even when the final answer is zero; a bad suffix does not automatically prevent exploring earlier branches.

Key Takeaway

When a string split consumes one of a fixed set of lengths, count completions by those disjoint choices. A prefix recurrence reading only a fixed number of preceding states can roll its storage, provided the numeric representation remains exact.