Decode Ways

Difficulty: Medium

A message of capital letters was encoded as numbers: A -> "1", B -> "2", ..., Z -> "26". You're given the digit string s.

There are no separators, so digits can sometimes be split into letters in more than one way ("11" could be "AA" or "K"). Count the ways s can be decoded.

A "0" that can't be part of a valid two-digit code (like a leading "0", or a "0" on its own) gives no valid decodings.

Examples

Input: s = "12"
Output: 2

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

Input: s = "226"
Output: 3

"BZ" (2, 26), "VF" (22, 6), or "BBF" (2, 2, 6).

Input: s = "06"
Output: 0

A leading "0" can't stand alone as a letter and "06" isn't a valid two-digit code, so there's no valid decoding at all.

Constraints

  • 1 <= s.length <= 100

  • s consists of digits only, and may contain leading zeros

Approach

Walk the string and ask at each position: how many ways can everything up to here be decoded? The last "chunk" of a decoding is one digit or two, so the count at position i comes from earlier counts:

  • If the digit just before i is a valid code (1 through 9), add the count from one position earlier.
  • If the pair of digits just before i is a valid code (10 through 26), add the count from two positions earlier.

It's the same "build up from smaller answers" shape as Climbing Stairs, with extra validity checks for the zero-digit edge cases.

Solutions

Brute Force — Plain Recursion

From each position, try taking one digit and (when valid) two digits, and recurse on the rest. Correct, but re-explores the same positions repeatedly.

function numDecodings(s) {
  const n = s.length;

  function ways(i) {
    if (i === n) return 1; // successfully consumed the whole string
    if (s[i] === "0") return 0; // no valid code starts with 0

    let total = ways(i + 1); // consume one digit

    if (i + 1 < n) {
      const twoDigit = Number(s.slice(i, i + 2));
      if (twoDigit >= 10 && twoDigit <= 26) {
        total += ways(i + 2); // consume two digits
      }
    }

    return total;
  }

  return ways(0);
}

Time: O(2^n) · Space: O(n) — recursion depth

Optimal — Bottom-Up Tabulation

Count the decodings of each prefix from smallest to largest, reusing the previous two counts instead of recursing.

function numDecodings(s) {
  const n = s.length;
  if (s[0] === "0") return 0;

  // dp[i] = number of ways to decode the first i characters of s
  const dp = new Array(n + 1).fill(0);
  dp[0] = 1; // empty prefix: exactly one way (decode nothing)
  dp[1] = 1; // first character is guaranteed non-zero by the check above

  for (let i = 2; i <= n; i++) {
    const oneDigit = Number(s[i - 1]);
    if (oneDigit >= 1) {
      dp[i] += dp[i - 1];
    }

    const twoDigit = Number(s.slice(i - 2, i));
    if (twoDigit >= 10 && twoDigit <= 26) {
      dp[i] += dp[i - 2];
    }
  }

  return dp[n];
}

Time: O(n) · Space: O(n) for the dp array (can be reduced to O(1) with two variables)