Skip to content
Decoder

Decode Ways

Medium

Problem

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.

Example 1

Input
s = "12"
Output
2
Why
"AB" or "L"

Example 2

Input
s = "226"
Output
3
Why
"BZ", "VF", "BBF"

Example 3

Input
s = "06"
Output
0
Why
no letter starts with 0

Example 4

Input
s = "11106"
Output
2
Why
1 1 10 6, 11 10 6

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.