-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path017. Letter Combinations of a Phone Number.py
63 lines (54 loc) · 1.71 KB
/
017. Letter Combinations of a Phone Number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Given a digit string, return all possible letter combinations that the number could represent.
# A mapping of digit to letters (just like on the telephone buttons) is given below.
# Input:Digit string "23"
# Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
# DFS backtracking
class Solution(object):
def letterCombinations(self, digits):
"""
O(2^n)
:type digits: str
:rtype: List[str]
"""
if not digits:
return []
res = []
dic = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz'
}
self.dfs(res, '',dic,digits,0)
return res
def dfs(self, res, temp, dic,digits,index):
if len(temp) == len(digits):
res.append(temp)
return
# focus on !
# digits[index] -> 2: generate a,b,c
for letter in dic[digits[index]]:
self.dfs(res, temp+letter, dic, digits, index+1)
class Solution2(object):
def letterCombinations(self, password):
if not password:
return []
res = []
dic = {'a':"12", 'c':"34"}
for char in password:
if char not in dic:
dic[char] = char
self.dfs(res, "", dic, password, 0)
return res
def dfs(self, res, temp, dic, password, index):
if index == len(password):
res.append(temp)
return
for letter in dic[password[index]]:
self.dfs(res, temp+letter, dic, password, index+1)
m = Solution2()
print m.letterCombinations('abc')