File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
leetcode.com 17. Letter Combinations of a Phone Number v2 Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+
4
+ class Solution :
5
+ def letterCombinations (self , digits : str ) -> List [str ]:
6
+ dict = {}
7
+ dict ['2' ] = ['a' , 'b' , 'c' ]
8
+ dict ['3' ] = ['d' , 'e' , 'f' ]
9
+ dict ['4' ] = ['g' , 'h' , 'i' ]
10
+ dict ['5' ] = ['j' , 'k' , 'l' ]
11
+ dict ['6' ] = ['m' , 'n' , 'o' ]
12
+ dict ['7' ] = ['p' , 'q' , 'r' , 's' ]
13
+ dict ['8' ] = ['t' , 'u' , 'v' ]
14
+ dict ['9' ] = ['w' , 'x' , 'y' , 'z' ]
15
+
16
+ queue = [['' ]]
17
+ queue2 = []
18
+
19
+ for d in digits :
20
+ for prev in queue :
21
+ for new in dict [d ]:
22
+ queue2 .append (prev + [new ])
23
+
24
+ queue = queue2
25
+ queue2 = []
26
+
27
+ answer = []
28
+ for q in queue :
29
+ a = '' .join (q )
30
+ if a != '' :
31
+ answer .append (a )
32
+
33
+ return answer
You can’t perform that action at this time.
0 commit comments