L3 · FAANGDynamic programming~15 min · 7 tests

Decode Ways (A=1 … Z=26)

Count the ways to decode a digit string where A=1 … Z=26 using a two-variable DP in O(n), handling the tricky zeros. A frequent Meta and Google question.

The problem

Letters are encoded as "1" → A … "26" → Z. Return the number of ways to decode the digit string s. A part like "06" is not valid.

Examples

  1. Example 1

    Input

    num_decodings('12')

    Expected output

    2
  2. Example 2

    Input

    num_decodings('226')

    Expected output

    3

+ 5 hidden tests on Submit — leading zero.

Edge cases to ask about

  • Leading zero
  • '10' and '20'
  • '30' is invalid

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · num_decodings
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    Measure it

    Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Recursion over 1- and 2-digit stepsO(2ⁿ)O(n)
    bestDP like FibonacciO(n)O(1)ways[i] = ways[i−1] (if s[i] ≠ '0') + ways[i−2] (if s[i−1:i+1] is 10–26).
    Walkthrough of the optimal approach (try it yourself first)

    ways[i] = number of decodings of the first i + 1 digits. The last step used either one digit (valid if it isn't "0") or two (valid if 10–26), so ways[i] = ways[i−1]·[single ok] + ways[i−2]·[pair ok]. Only the previous two values matter — Fibonacci-shaped, O(1) space.

    Zeros are the whole difficulty: "10" → 1, "100" → 0, "06" → 0.

    Complexity: O(n) time, O(1) space. One pass; only the last two counts are kept.

    Reveal the reference solution
    def num_decodings(s):
        if not s:
            return 0
        two_back, one_back = 1, 1 if s[0] != "0" else 0
        for i in range(1, len(s)):
            cur = 0
            if s[i] != "0":
                cur += one_back
            if 10 <= int(s[i - 1:i + 1]) <= 26:
                cur += two_back
            two_back, one_back = one_back, cur
        return one_back

    Follow-ups interviewers ask

    • '*' can be any digit 1–9 (Decode Ways II).

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Decode Ways (A=1 … Z=26) in Python?

    The optimal solution runs in O(n) time and O(1) auxiliary space. One pass; only the last two counts are kept.

    What is the brute-force approach, and how do you optimise it?

    Recursion over 1- and 2-digit steps: O(2ⁿ) time, O(n) space. DP like Fibonacci: O(n) time, O(1) space. ways[i] = ways[i−1] (if s[i] ≠ '0') + ways[i−2] (if s[i−1:i+1] is 10–26).

    What follow-up questions do interviewers ask about Decode Ways (A=1 … Z=26)?

    '*' can be any digit 1–9 (Decode Ways II).