-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path017 Letter Combinations of a Phone Number.py
More file actions
43 lines (36 loc) · 1.12 KB
/
017 Letter Combinations of a Phone Number.py
File metadata and controls
43 lines (36 loc) · 1.12 KB
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
"""
Given a digit string, return all possible letter combinations that the number could represent.
Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
Author : Rajeev Ranjan
"""
class Solution:
digit2letters = {
'2': "abc",
'3': "def",
'4': "ghi",
'5': "jkl",
'6': "mno",
'7': "pqrs",
'8': "tuv",
'9': "wxyz"
}
def letterCombinations(self, digits):
"""
DFS
:param digits: str
:return: a list of strings, [s1, s2]
"""
result = []
self.dfs_traverse(digits, "", result)
return result
def dfs_traverse(self, string_seq, current, result):
if not string_seq:
result.append(current)
return
for letter in self.digit2letters[string_seq[0]]:
self.dfs_traverse(string_seq[1:], current+letter, result)
if __name__=="__main__":
print Solution().letterCombinations("23")