InferenceAI

Enter at least two characters to search.

Start free

Phone Letter Combinations

mediumRecursion & Backtracking25 min25 XP

On a phone keypad, 2 = abc, 3 = def, 4 = ghi, 5 = jkl, 6 = mno, 7 = pqrs, 8 = tuv, 9 = wxyz. Given a string of digits 2–9, return every letter combination it could represent, in lexicographic order. Return [] for an empty string.

Examples

EXAMPLE WALKTHROUGHtwo digits

A guided view of the published example, not a trace of your code.

INPUT["23"]
TASKphone_letter_combinations()
EXPECTED OUTPUT["ad","ae","af","bd","be","bf","cd","ce","cf"]

Start with the input values shown in this example.

Example 1 of 3

Input: digits = "23"

Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]

Explanation: two digits

Input: digits = ""

Output: []

Explanation: empty

Input: digits = "2"

Output: ["a","b","c"]

Explanation: one digit

Hint 1

Build the answers one digit at a time, extending every partial string.

Topics

DSARecursion & BacktrackingPython