-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path784-Letter-Case-Permutation.swift
More file actions
41 lines (32 loc) · 1.09 KB
/
784-Letter-Case-Permutation.swift
File metadata and controls
41 lines (32 loc) · 1.09 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
//
// 784-Letter-Case-Permutation.swift
//
//
// Created by Lugick Wang on 2021/1/29.
//
import Foundation
class Solution {
func letterCasePermutation(_ S: String) -> [String] {
var characters = S.map({String($0)})
var results = [String]()
backtracking(array: &characters, index: 0, results: &results)
return results
}
func backtracking(array:inout [String], index: Int, results:inout [String]) {
if index == array.count {
results.append(array.joined(separator: ""))
return
}
//不处理的分支直接下一层
backtracking(array: &array, index: index+1, results: &results)
//处理的分支
if Character(array[index]).isLetter {
print(index)
if let ascii = array[index].unicodeScalars.first?.value {
array[index] = String(UnicodeScalar(UInt8(ascii^(1<<5))))
backtracking(array: &array, index: index+1, results: &results)
}
}
}
}
Solution().letterCasePermutation("a1b2")