Decode MessageLoading saved progress…

Decode Message

A message was encoded by mapping each letter to its position in the alphabet — A to 1, B to 2, all the way to Z to 26 — and then writing the numbers down with no separators. Given the resulting string of digits, return the number of distinct ways it could be decoded back into letters. This is the classic Decode Ways counting problem: you are not asked to list the decodings, only to count them.

Signature

function stringDecodeMessage(digits: string): number;
// digits is a string of '0'–'9' characters; returns a count of valid decodings

Examples

stringDecodeMessage("12");  // 2  — "1 2" → "AB", or "12" → "L"
stringDecodeMessage("226"); // 3  — "2 2 6" → "BBF", "22 6" → "VF", "2 26" → "BZ"
stringDecodeMessage("06");  // 0  — no decoding: "0" maps to nothing, and "06" is not a valid pair

Notes

  • Mapping is 1–26. A single digit decodes only if it is 19. A two-digit chunk decodes only if it is 1026. Anything outside those ranges contributes nothing.
  • Leading zeros are invalid. A pair like "06" or "08" does not map to a letter — only "10" through "26" are valid two-digit chunks. 09 is not 9.
  • A lone "0" has no decoding. stringDecodeMessage("0") returns 0, and any 0 that cannot be the second digit of a 1026 pair kills every decoding that reaches it.
  • You return a count, not the decodings themselves. Listing them is a separate, harder problem (see the solution's Going further).
  • Empty input. stringDecodeMessage("") returns 1 — the empty string has exactly one decoding, the empty message. This is the base case the counting relies on; the solution explains why.
Loading editor…