 All Problems
Letter Combinations of a Phone Number
medium
backtracking
hash map
string
amazon
facebook
google

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answers in lexicographic order.

The mapping of digits to letters (just like telephone buttons) is:

  • 2 → abc, 3 → def, 4 → ghi, 5 → jkl, 6 → mno, 7 → pqrs, 8 → tuv, 9 → wxyz

Example 1:

Input: 23
Output:
ad
ae
af
bd
be
bf
cd
ce
cf

Example 2:

Input: 2
Output:
a
b
c

Constraints:

  • 0 ≤ digits.length ≤ 4
  • digits[i] is a digit in the range ['2', '9'].

Input format: A single string of digits (no spaces).

Output format: All letter combinations, one per line, in lexicographic order. If input is empty, print nothing.

Run to check your code against the sample cases, or submit to run every case