-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathletter-combinations-of-a-phone-number.py
77 lines (67 loc) · 2.12 KB
/
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
64
65
66
67
68
69
70
71
72
73
74
75
76
# Time: O(n * 4^n)
# Space: O(1)
# iterative solution
class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if not digits:
return []
lookup = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
total = 1
for digit in digits:
total *= len(lookup[int(digit)])
result = []
for i in xrange(total):
base, curr = total, []
for digit in digits:
choices = lookup[int(digit)]
base //= len(choices)
curr.append(choices[(i//base)%len(choices)])
result.append("".join(curr))
return result
# Time: O(n * 4^n)
# Space: O(1)
# iterative solution
class Solution2(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if not digits:
return []
result = [""]
lookup = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
for digit in reversed(digits):
choices = lookup[int(digit)]
m, n = len(choices), len(result)
result.extend([result[i % n] for i in xrange(n, m*n)])
for i in xrange(m*n):
result[i] = choices[i//n] + result[i]
return result
# Time: O(n * 4^n)
# Space: O(n)
# recursive solution
class Solution3(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
lookup = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
def letterCombinationsRecu(result, digits, curr, n):
if n == len(digits):
result.append("".join(curr))
return
for choice in lookup[int(digits[n])]:
curr.append(choice)
letterCombinationsRecu(result, digits, curr, n+1)
curr.pop()
if not digits:
return []
result = []
letterCombinationsRecu(result, digits, [], 0)
return result