-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path17_LetterCombinationsPN.py
executable file
·48 lines (40 loc) · 1.21 KB
/
17_LetterCombinationsPN.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
phone_letters = {0: [" "],
1: ["*"],
2: ["a", "b", "c"],
3: ["d", "e", "f"],
4: ["g", "h", "i"],
5: ["j", "k", "l"],
6: ["m", "n", "o"],
7: ["p", "q", "r", "s"],
8: ["t", "u", "v"],
9: ["w", "x", "y", "z"],
}
if digits:
all_str = phone_letters[ord(digits[0]) - ord("0")]
else:
return []
for i in range(1, len(digits)):
all_str = self.combination(
all_str,
phone_letters[ord(digits[i]) - ord("0")])
return all_str
# return string which combines a in str_a with b in str_b
def combination(self, str_a, str_b):
combine_str = []
for a in str_a:
for b in str_b:
combine_str.append(a + b)
return combine_str
"""
""
"37"
"1234"
"""