Skills101
← All coding problems

Letter Combinations of a Phone Number

mediumRecursion & Backtracking+20 XPTime limit 2s

On a phone keypad, each digit 2–9 maps to letters: 2 → abc, 3 → def, 4 → ghi, 5 → jkl, 6 → mno, 7 → pqrs, 8 → tuv, 9 → wxyz. Print every letter string the digits could represent.

Input: one line of digits (each 2–9).

Output: all combinations, one per line, in alphabetical order.

Constraints: 1 ≤ number of digits ≤ 6.

Think about it: one level of recursion per digit, one branch per letter. Trying letters in keypad order already gives alphabetical output.

Sample input 1

23

Sample output 1

ad
ae
af
bd
be
bf
cd
ce
cf

Sample input 2

2

Sample output 2

a
b
c

📖 Stuck? Learn the pattern

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.