Letter Combinations of a Phone Number

Medium#17letter by letter

On an old phone keypad each digit from 2 to 9 stands for three or four letters. You get a string of such digits; return every word you can type with it, one letter per digit, in any order, and nothing for an empty string. Our example: "38", where 3 is d, e, f and 8 is t, u, v, gives 9 words: dt, du, dv, et, eu, ev, ft, fu, fv.

phone keypad
1—2abc3def
4ghi5jkl6mno
7pqrs8tuv9wxyz
1 and 0 have no letters
input
digits: a string of digits from '2' to '9', maybe empty; each digit stands for the letters of its key on the keypad above.
returns
Every string the digits can type: one letter from each digit's button, in the order of the digits. For an empty digits, an empty list. The strings in any order.
constraints
  • 0 ≤ digits.length ≤ 4
  • digits[i]: a digit from '2' to '9'
our 10 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {string} digits
 * @return {string[]}
 */
function letterCombinations(digits) {

}
Ctrl ↵
format
reset

saved in this browser

10 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more backtracking problems

Pattern “Backtracking” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.