Back to Practice
#0280

Decode Ways

MediumDSA28 min20 XP

Problem

Digits map 1 -> A through 26 -> Z. Return the number of valid decodings for s.

Why This Matters

Decode Ways is a compact DP problem where invalid zero handling matters more than the recurrence itself.

Function Signature

def num_decodings(s):

Examples

Example 1
Inputs = "226"
Output3

226 can decode as 2-2-6, 22-6, or 2-26.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Three decodings
Input"226"
Expected3

There are three valid splits.

Leading zero
Input"06"
Expected0

No code starts with zero.

Hidden Test Categories
string with 10string with 30long repeated ones