150 najlepszych wywiadówŁatwe

Kombinacje liter numeru telefonu

Szczegółowy przewodnik i implementacja Python dla problemu „Kombinacje liter w numerze telefonu”.

Oświadczenie o problemie

Łatwe

Biorąc pod uwagę ciąg zawierający cyfry od 2 do 9 włącznie, zwróć wszystkie możliwe kombinacje liter, które może reprezentować ta liczba. Zwróć odpowiedź w dowolnej kolejności.

Poniżej podano odwzorowanie cyfr na litery (tak jak na przyciskach telefonu). Należy pamiętać, że 1 nie jest odwzorowywana na żadne litery.

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

Zaimplementuj funkcję letterCombinations(digits: str) -> list.

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

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla cofania, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.