FastPrepPhone Keypad Letter Combinations
Problem · String

Phone Keypad Letter Combinations

Learn this problem
MediumGoogle logoGoogleFULLTIMEONSITE INTERVIEW
See Google hiring insights

Problem statement

Given a string digits, return every letter combination that the digits could represent on a conventional telephone keypad.

  • 2 maps to abc, 3 to def, 4 to ghi, 5 to jkl.
  • 6 maps to mno, 7 to pqrs, 8 to tuv, and 9 to wxyz.

For this exercise, emit combinations in depth-first order, visiting each digit's mapped letters from left to right. Return an empty list for empty input.

Function

letterCombinations(digits: String) → List<String>

Examples

Example 1

digits = "23"return = ["ad","ae","af","bd","be","bf","cd","ce","cf"]

Choose one of abc for the first position and one of def for the second, following left-to-right depth-first order.

Example 2

digits = ""return = []

There is no position to assign, so the exercise returns an empty list.

Constraints

  • 0 <= digits.length <= 4
  • Every character in digits is between 2 and 9.

More Google problems

drafts saved locally
public List<String> letterCombinations(String digits) {
    // Write your code here.
}
digits"23"
expected["ad","ae","af","bd","be","bf","cd","ce","cf"]
checking account