data-structures · intermediate · ~15 min

Phone letter combinations

Count keypad letter strings.

Challenge

Implement:

long long count_letter_combinations(const char *digits);

On a phone keypad (2->3 letters, ..., 7 and 9 -> 4 letters), return the number of possible letter strings for a digit string. Digits 0 or 1, or an empty string, yield 0.

Input format

A digit string.

Output format

Number of letter strings.

Constraints

Multiply the choices per digit.

Starter code

#include <stddef.h>
/* Phone keypad: number of letter strings for a digit string (2->3 letters ... 9->4). Digits 0 or 1, or an empty string, yield 0. */
long long count_letter_combinations(const char *digits){ (void)digits; return 0; }

Common mistakes

Assigning the wrong letter counts (7 and 9 have 4, not 3).

Edge cases to handle

Empty or containing 0/1 -> 0.

Background lessons

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