Letter Combinations of a Phone Number

Given a string digits containing digits from 2 to 9 inclusive, return all possible letter combinations that the number could represent.

Use the same digit-to-letter mapping as on a telephone keypad:

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

Return the combinations in any order.

Example 1
Inputdigits = "23"
Output["ad","ae","af","bd","be","bf","cd","ce","cf"]
Digit 2 maps to abc and digit 3 maps to def, so every pair formed by one letter from each group is included.
Example 2
Inputdigits = "2"
Output["a","b","c"]
There are no digits to map, so there are no possible letter combinations.

Constraints

  • 1 <= digits.length <= 4
  • digits[i] is a digit in the range ['2', '9']

Asked at 40 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate