forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0017.py
55 lines (48 loc) · 1.43 KB
/
0017.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
# class Solution:
# def __init__(self):
# self.letterMap = [
# ' ',
# '',
# 'abc',
# 'def',
# 'ghi',
# 'jkl',
# 'mno',
# 'pqrs',
# 'tuv',
# 'wxyz'
# ]
# def findCombination(self, digits, index, s, res):
# if index == len(digits):
# res.append(s)
# return
# char = digits[index]
# letters = self.letterMap[ord(char) - ord('0')]
# for letter in letters:
# self.findCombination(digits, index + 1, s + letter, res)
# def letterCombinations(self, digits):
# """
# :type digits: str
# :rtype: List[str]
# """
# result = list()
# if not digits:
# return result
# self.findCombination(digits, 0, "", result)
# return result
import itertools
class Solution:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if not digits:
return []
l_map = {'2':'abc', '3':'def', '4':'ghi', '5':'jkl',
'6':'mno', '7':'pqrs', '8':'tuv', '9':'wxyz'}
chars = [l_map.get(d) for d in digits]
return [''.join(x) for x in itertools.product(*chars)]
if __name__ == '__main__':
digits = '23'
print(Solution().letterCombinations(digits))