AlgoFrames

Decode ways

Standard · Write code · about 15 min

Letters are encoded as numbers: A = 1, B = 2, up to Z = 26. Given a string s of digits, return how many ways it can be decoded back into letters.

A 0 cannot stand alone, and a two-digit group may not start with 0. So 06 is not a valid letter.

Example 1

Input:

["226"]

Output:

3

BBF (2 2 6), BZ (2 26) or VF (22 6).

Example 2

Input:

["12"]

Output:

2

AB (1 2) or L (12).

Constraints

  • 1 ≤ s.length ≤ 100
  • s contains only digits
  • The answer fits in a 32-bit signed integer

Learn the idea first: 1-D DP: house robber, decode ways, jump game II, a visual lesson.

More 1-D DP: house robber, decode ways, jump game II problems