File tree Expand file tree Collapse file tree 6 files changed +174
-0
lines changed
01.04.Palindrome Permutation Expand file tree Collapse file tree 6 files changed +174
-0
lines changed Original file line number Diff line number Diff line change @@ -120,6 +120,24 @@ impl Solution {
120
120
}
121
121
```
122
122
123
+ ``` swift
124
+ class Solution {
125
+ func canPermutePalindrome (_ s : String ) -> Bool {
126
+ var cnt = [Character : Int ]()
127
+ for char in s {
128
+ cnt[char, default : 0 ] += 1
129
+ }
130
+
131
+ var sum = 0
132
+ for count in cnt.values {
133
+ sum += count % 2
134
+ }
135
+
136
+ return sum < 2
137
+ }
138
+ }
139
+ ```
140
+
123
141
<!-- tabs: end -->
124
142
125
143
### 方法二:哈希表的另一种实现
Original file line number Diff line number Diff line change @@ -117,6 +117,24 @@ impl Solution {
117
117
}
118
118
```
119
119
120
+ ``` swift
121
+ class Solution {
122
+ func canPermutePalindrome (_ s : String ) -> Bool {
123
+ var cnt = [Character : Int ]()
124
+ for char in s {
125
+ cnt[char, default : 0 ] += 1
126
+ }
127
+
128
+ var sum = 0
129
+ for count in cnt.values {
130
+ sum += count % 2
131
+ }
132
+
133
+ return sum < 2
134
+ }
135
+ }
136
+ ```
137
+
120
138
<!-- tabs: end -->
121
139
122
140
### Solution 2: Another Implementation of Hash Table
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func canPermutePalindrome( _ s: String ) -> Bool {
3
+ var cnt = [ Character: Int] ( )
4
+ for char in s {
5
+ cnt [ char, default: 0 ] += 1
6
+ }
7
+
8
+ var sum = 0
9
+ for count in cnt. values {
10
+ sum += count % 2
11
+ }
12
+
13
+ return sum < 2
14
+ }
15
+ }
Original file line number Diff line number Diff line change @@ -223,6 +223,48 @@ impl Solution {
223
223
}
224
224
```
225
225
226
+ ``` swift
227
+ class Solution {
228
+ func oneEditAway (_ first : String , _ second : String ) -> Bool {
229
+ let m = first.count , n = second.count
230
+ if m < n {
231
+ return oneEditAway (second, first)
232
+ }
233
+ if m - n > 1 {
234
+ return false
235
+ }
236
+
237
+ var cnt = 0
238
+ var firstIndex = first.startIndex
239
+ var secondIndex = second.startIndex
240
+
241
+ if m == n {
242
+ while secondIndex != second.endIndex {
243
+ if first[firstIndex] != second[secondIndex] {
244
+ cnt += 1
245
+ if cnt > 1 {
246
+ return false
247
+ }
248
+ }
249
+ firstIndex = first.index (after : firstIndex)
250
+ secondIndex = second.index (after : secondIndex)
251
+ }
252
+ return true
253
+ } else {
254
+ while firstIndex != first.endIndex {
255
+ if secondIndex == second.endIndex || (secondIndex != second.endIndex && first[firstIndex] != second[secondIndex]) {
256
+ cnt += 1
257
+ } else {
258
+ secondIndex = second.index (after : secondIndex)
259
+ }
260
+ firstIndex = first.index (after : firstIndex)
261
+ }
262
+ }
263
+ return cnt < 2
264
+ }
265
+ }
266
+ ```
267
+
226
268
<!-- tabs: end -->
227
269
228
270
<!-- end -->
Original file line number Diff line number Diff line change @@ -231,6 +231,48 @@ impl Solution {
231
231
}
232
232
```
233
233
234
+ ``` swift
235
+ class Solution {
236
+ func oneEditAway (_ first : String , _ second : String ) -> Bool {
237
+ let m = first.count , n = second.count
238
+ if m < n {
239
+ return oneEditAway (second, first)
240
+ }
241
+ if m - n > 1 {
242
+ return false
243
+ }
244
+
245
+ var cnt = 0
246
+ var firstIndex = first.startIndex
247
+ var secondIndex = second.startIndex
248
+
249
+ if m == n {
250
+ while secondIndex != second.endIndex {
251
+ if first[firstIndex] != second[secondIndex] {
252
+ cnt += 1
253
+ if cnt > 1 {
254
+ return false
255
+ }
256
+ }
257
+ firstIndex = first.index (after : firstIndex)
258
+ secondIndex = second.index (after : secondIndex)
259
+ }
260
+ return true
261
+ } else {
262
+ while firstIndex != first.endIndex {
263
+ if secondIndex == second.endIndex || (secondIndex != second.endIndex && first[firstIndex] != second[secondIndex]) {
264
+ cnt += 1
265
+ } else {
266
+ secondIndex = second.index (after : secondIndex)
267
+ }
268
+ firstIndex = first.index (after : firstIndex)
269
+ }
270
+ }
271
+ return cnt < 2
272
+ }
273
+ }
274
+ ```
275
+
234
276
<!-- tabs: end -->
235
277
236
278
<!-- end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func oneEditAway( _ first: String , _ second: String ) -> Bool {
3
+ let m = first. count, n = second. count
4
+ if m < n {
5
+ return oneEditAway ( second, first)
6
+ }
7
+ if m - n > 1 {
8
+ return false
9
+ }
10
+
11
+ var cnt = 0
12
+ var firstIndex = first. startIndex
13
+ var secondIndex = second. startIndex
14
+
15
+ if m == n {
16
+ while secondIndex != second. endIndex {
17
+ if first [ firstIndex] != second [ secondIndex] {
18
+ cnt += 1
19
+ if cnt > 1 {
20
+ return false
21
+ }
22
+ }
23
+ firstIndex = first. index ( after: firstIndex)
24
+ secondIndex = second. index ( after: secondIndex)
25
+ }
26
+ return true
27
+ } else {
28
+ while firstIndex != first. endIndex {
29
+ if secondIndex == second. endIndex || ( secondIndex != second. endIndex && first [ firstIndex] != second [ secondIndex] ) {
30
+ cnt += 1
31
+ } else {
32
+ secondIndex = second. index ( after: secondIndex)
33
+ }
34
+ firstIndex = first. index ( after: firstIndex)
35
+ }
36
+ }
37
+ return cnt < 2
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments