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.