File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private let numberLetters : [ Character : String ] = [
3
+ " 2 " : " abc " ,
4
+ " 3 " : " def " ,
5
+ " 4 " : " ghi " ,
6
+ " 5 " : " jkl " ,
7
+ " 6 " : " mno " ,
8
+ " 7 " : " pqrs " ,
9
+ " 8 " : " tuv " ,
10
+ " 9 " : " wxyz "
11
+ ]
12
+ func letterCombinations( _ digits: String ) -> [ String ] {
13
+ guard !digits. isEmpty else { return [ ] }
14
+ var result : [ String ] = [ ]
15
+ var digits = digits. map { $0 }
16
+ func generateCombinations( _ index: Int , _ path: String ) {
17
+ guard index < digits. count else {
18
+ result. append ( path)
19
+ return
20
+ }
21
+ let digit = digits [ index]
22
+ if let letters = numberLetters [ digit] {
23
+ for letter in letters {
24
+ generateCombinations ( index + 1 , path + " \( letter) " )
25
+ }
26
+ }
27
+ }
28
+ generateCombinations ( 0 , " " )
29
+ return result
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments