data-structures · intermediate · ~15 min

Decode ways

Count decodings of a digit string (1->A..26->Z).

Challenge

Implement:

long long decode_ways(const char *s);

Count the ways to decode a digit string where 1->A, ..., 26->Z. A standalone or leading '0' is invalid.

Input format

A digit string.

Output format

Number of decodings.

Constraints

A '0' must pair with a preceding 1 or 2.

Starter code

#include <stddef.h>
/* Number of ways to decode a digit string where 1->A ... 26->Z. A leading/standalone 0 is invalid. */
long long decode_ways(const char *s){ (void)s; return 0; }

Common mistakes

Mishandling '0' (it can never stand alone and only forms 10 or 20).

Edge cases to handle

Leading '0' -> 0; "10" -> 1.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.