-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17-letter-combinations-of-a-phone-number.swift
48 lines (36 loc) · 1.33 KB
/
17-letter-combinations-of-a-phone-number.swift
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
//
// 17-letter-combinations-of-a-phone-number.swift
//
//
// Created by Lugick Wang on 2020/12/30.
//
import Foundation
class Solution {
func letterCombinations(_ digits: String) -> [String] {
let array = ["","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]
var routeString = ""
var results = [String]()
if digits.count == 0 {
return results
}
backtrack(&results, array: array, digits: digits, index: 0, routeString: &routeString)
return results
}
func backtrack(_ results:inout [String], array:[String], digits: String, index: Int, routeString:inout String) {
if index == digits.count {
results.append(routeString)
return;
}
let numberIndex = Int(String(Array(digits)[index])) ?? 0
let characters = Array(array[numberIndex])
for i in 0..<characters.count {
let str = characters[i]
routeString += String(str)
print(routeString)
backtrack(&results, array: array, digits: digits, index: index+1, routeString: &routeString)
routeString.remove(at: routeString.index(before: routeString.endIndex))
print("drop")
print(routeString)
}
}
}