Skip to content

Commit 696d844

Browse files
authored
feat: add solutions to lcci problems: No.01.04,01.05 (doocs#2577)
1 parent 1345b3c commit 696d844

File tree

6 files changed

+174
-0
lines changed

6 files changed

+174
-0
lines changed

lcci/01.04.Palindrome Permutation/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,24 @@ impl Solution {
120120
}
121121
```
122122

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+
123141
<!-- tabs:end -->
124142

125143
### 方法二:哈希表的另一种实现

lcci/01.04.Palindrome Permutation/README_EN.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ impl Solution {
117117
}
118118
```
119119

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+
120138
<!-- tabs:end -->
121139

122140
### Solution 2: Another Implementation of Hash Table
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

lcci/01.05.One Away/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,48 @@ impl Solution {
223223
}
224224
```
225225

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+
226268
<!-- tabs:end -->
227269

228270
<!-- end -->

lcci/01.05.One Away/README_EN.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,48 @@ impl Solution {
231231
}
232232
```
233233

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+
234276
<!-- tabs:end -->
235277

236278
<!-- end -->

lcci/01.05.One Away/Solution.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

0 commit comments

Comments
 (0)