List every letter combination a string of phone keypad digits can spell

Quick Overview

Given a string of digits from 2 to 9, return every letter string those digits can spell on a standard phone keypad, in lexicographic order. The problem tests backtracking or level-by-level expansion, building a Cartesian product, and handling the empty input.

List every letter combination a string of phone keypad digits can spell

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Each digit maps to a set of letters like on a phone keypad (`2 -> "abc"`, `3 -> "def"`, and so on). ### Function Signature ```python def letter_combinations(digits: str) -> list[str]: ``` ### Rules - The keypad mapping is `2 -> "abc"`, `3 -> "def"`, `4 -> "ghi"`, `5 -> "jkl"`, `6 -> "mno"`, `7 -> "pqrs"`, `8 -> "tuv"`, `9 -> "wxyz"`. - A combination picks one letter for each digit, keeping the letters in the same order as the digits. Its length equals `len(digits)`. - Return the combinations sorted in ascending lexicographic order. - If `digits` is empty, return an empty list. ### Constraints - `0 <= len(digits) <= 4` - Each `digits[i]` is a digit in the range `'2'` to `'9'`. ### Examples ```text Input: digits = "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] ``` Digit 2 offers `a`, `b`, `c` and digit 3 offers `d`, `e`, `f`, so there are 3 * 3 = 9 two-letter combinations, listed in sorted order. ```text Input: digits = "234" Output: ["adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"] ``` Each of the three digits has three letters, giving 27 combinations. Every combination starting with `a` comes before those starting with `b`, and so on. ```text Input: digits = "" Output: [] ``` An empty input has no combinations.

Overview: Given a string of digits from 2 to 9, return every letter string those digits can spell on a standard phone keypad, in lexicographic order. The problem tests backtracking or level-by-level expansion, building a Cartesian product, and handling the empty input.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Each digit maps to a set of letters like on a phone keypad (2 -> "abc", 3 -> "def", and so on).

Function Signature

def letter_combinations(digits: str) -> list[str]:

Rules

  • The keypad mapping is 2 -> "abc" , 3 -> "def" , 4 -> "ghi" , 5 -> "jkl" , 6 -> "mno" , 7 -> "pqrs" , 8 -> "tuv" , 9 -> "wxyz" .
  • A combination picks one letter for each digit, keeping the letters in the same order as the digits. Its length equals len(digits) .
  • Return the combinations sorted in ascending lexicographic order.
  • If digits is empty, return an empty list.

Constraints

  • 0 <= len(digits) <= 4
  • Each digits[i] is a digit in the range '2' to '9' .

Examples

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

Digit 2 offers a, b, c and digit 3 offers d, e, f, so there are 3 * 3 = 9 two-letter combinations, listed in sorted order.

Input: digits = "234"
Output: ["adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"]

Each of the three digits has three letters, giving 27 combinations. Every combination starting with a comes before those starting with b, and so on.

Input: digits = ""
Output: []

An empty input has no combinations.