FastPrepPhone-Number Letter Combinations

Phone-Number Letter Combinations

Headway logoHeadway● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a string digits, return every letter combination represented by its digits on a telephone keypad.

For this exercise, assume the standard mapping:

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

Choose exactly one mapped letter for each digit, preserving the digit positions. Return all combinations in ascending lexicographic order. For this exercise, assume an empty digits string returns an empty array, not an array containing an empty string.

Function

letterCombinations(digits: String) → String[]

Examples

Example 1

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

Choose one letter from abc, then one from def. Listing the choices in ascending order gives nine combinations.

Example 2

digits = ""return = []

The empty-input rule returns no combinations.

Example 3

digits = "79"return = ["pw","px","py","pz","qw","qx","qy","qz","rw","rx","ry","rz","sw","sx","sy","sz"]

Both digits have four letters. Each letter from pqrs is paired with every letter from wxyz, producing sixteen ordered combinations.

Constraints

  • 0 <= digits.length <= 4.
  • Every character in digits is a digit from 2 through 9.

More Headway problems

See Headway hiring insights
public String[] letterCombinations(String digits) {
    // Write your code here.
}
digits"23"
expected["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
Checking account…