35 · Dynamic Programming

#91 Decode Ways

Mediumsolve on LeetCode ↗

frequently reported atMicrosoftMetaAmazonGoogle

the problem

In plain words

Letters map to numbers: A = 1, …, Z = 26. Given a digit string, count how many ways it can be decoded back into letters. "06" is not valid (no letter is written with a leading zero).

in s = "12"

out 2

"AB" (1 2) or "L" (12).

in s = "226"

out 3

2 2 6, 22 6, 2 26.

in s = "06"

out 0

  • 1 ≤ s.length ≤ 100
  • s contains only digits, possibly with leading zeros
  • The answer fits in a 32-bit int

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #70
      Climbing Stairs
      Easyin Dynamic Programming
    2. #139
      Word Break
      Mediumin Dynamic Programming