Skip to content

Decode Ways

MediumDynamic Programming
Decode it first. Clues, constraints, pattern and plan before any code.

A message of capital letters was encoded with A → "1", B → "2", ..., Z → "26", then the digits were joined together. Given the digit string s, return how many different messages it could have come from. A code never has a leading zero, so "06" is not a valid letter.

s = "12"     →  2   ("AB" or "L")
s = "226"    →  3   ("BZ", "VF", "BBF")
s = "06"     →  0   (no letter starts with 0)
s = "11106"  →  2   (1 1 10 6, 11 10 6)

Constraints

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