Le migliori 150 intervisteFacile

Combinazioni di lettere di un numero di telefono

Guida dettagliata e implementazione Python per il problema "Combinazioni di lettere di un numero di telefono".

Dichiarazione del problema

Facile

Data una stringa contenente cifre da 2 a 9 incluse, restituisce tutte le possibili combinazioni di lettere che il numero potrebbe rappresentare. Riporta la risposta in qualsiasi ordine.

Di seguito viene fornita una mappatura delle cifre in lettere (proprio come sui pulsanti del telefono). Tieni presente che 1 non corrisponde ad alcuna lettera.

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

Implementa una funzione letterCombinations(digits: str) -> list.

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

Esempi

Example 1
Input
"23"
Output
["ad","ae","af","bd","be","bf","cd","ce","cf"]
Explanation

Digit 2 maps to 'abc' and digit 3 maps to 'def'. All combinations of one letter from each digit are generated.

Example 2
Input
""
Output
[]
Explanation

Empty input produces no combinations.

Example 3
Input
"2"
Output
["a","b","c"]
Explanation

Digit 2 maps to 'abc'.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche del backtracking come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Apri nell'editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.