data-structures · intermediate · ~15 min
Count decodings of a digit string (1->A..26->Z).
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.
A digit string.
Number of decodings.
A '0' must pair with a preceding 1 or 2.
#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; }
Mishandling '0' (it can never stand alone and only forms 10 or 20).
Leading '0' -> 0; "10" -> 1.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.