# Letter combination of telephone numbers

A given contains only numbers   2-9   String that returns all the letter combinations it can represent. The answers can be returned in any order.

The number to letter mapping is given as follows (the same as the telephone key). Note that 1 does not correspond to any letter. Example 1:

Input: digits = "23"
Example 2:

Input: digits = ""
Output: []
Example 3:

Input: digits = "2"
Output: ["a","b","c"]

Source: LeetCode

Since there are only four digits at most, we have the stupidest way to write all four cases directly:

```def letterCombinations(digits: str):
if not digits:
return []
dict = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz"
}
n = len(digits)
li = set()
if n == 1:
return [x for x in dict[digits]]
elif n == 2:
for i in range(n):
r = i + 1
while r < n:
for j in range(len(dict[digits[i]])):
for k in range(len(dict[digits[r]])):
r += 1
elif n == 3:
li.clear()
a = digits
b = digits
c = digits
for i in dict[a]:
for j in dict[b]:
for k in dict[c]:
else:
li.clear()
a = digits
b = digits
c = digits
d = digits
for i in dict[a]:
for j in dict[b]:
for k in dict[c]:
for z in dict[d]:
li.add(i + j + k + z)
return list(li)```

Of course, although this method works, it is too complex (but I think some others use this method to run very fast, which may be complicated by my writing). But there must be a better way to use it. Let's go on.

```def letterCombinations(self, digits: str) -> List[str]:
if not digits:
return []
dict = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz"
}
s = []
for i in digits:
s.append(dict[i])
result = []
import itertools
for i in itertools.product(*s):  # Adding a * sign before the list will split the list into independent elements one by one
result.append(''.join(i))
return result```

This method uses the product method in the itertools Library in python to directly iterate out all permutations and combinations. This method is very fast.

Execution result: passed

Execution time: 28 ms, at all   Python3   Defeated 87.12% of users in submission

Memory consumption: 14.8 MB in all   Python3   Defeated 94.23% of users in submission

Pass test case: 25  /  twenty-five

The last method is the most official one: backtracking.

```def letterCombinations(digits: str):
if not digits:
return list()

phoneMap = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
}

def backtrack(index: int):
if index == len(digits):
combinations.append("".join(combination))
else:
digit = digits[index]
for letter in phoneMap[digit]:
combination.append(letter)
backtrack(index + 1)
combination.pop()

combination = list()
combinations = list()
backtrack(0)
return combinations```

We must first know what backtracking is: backtracking is also called heuristics. The basic method of backtracking method is depth first search. It is an organized exhaustive search algorithm that can avoid unnecessary repeated search. The basic idea of backtracking algorithm is: go forward from one way, go in if you can, go back if you can't, and try another way.

In this topic, we first enumerate digits, and then enumerate the letters in phoneMap. Then, through the depth first search, all the permutations are exhausted. You can debug and personally feel the essence of backtracking method.

Posted on Wed, 01 Dec 2021 12:30:24 -0500 by britey