Skip to content

Commit

Permalink
feat: add solutions to lcci problems: No.01.04,01.05 (#2577)
Browse files Browse the repository at this point in the history
  • Loading branch information
klever34 authored Apr 13, 2024
1 parent 1345b3c commit 696d844
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lcci/01.04.Palindrome Permutation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ impl Solution {
}
```

```swift
class Solution {
func canPermutePalindrome(_ s: String) -> Bool {
var cnt = [Character: Int]()
for char in s {
cnt[char, default: 0] += 1
}

var sum = 0
for count in cnt.values {
sum += count % 2
}

return sum < 2
}
}
```

<!-- tabs:end -->

### 方法二:哈希表的另一种实现
Expand Down
18 changes: 18 additions & 0 deletions lcci/01.04.Palindrome Permutation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ impl Solution {
}
```

```swift
class Solution {
func canPermutePalindrome(_ s: String) -> Bool {
var cnt = [Character: Int]()
for char in s {
cnt[char, default: 0] += 1
}

var sum = 0
for count in cnt.values {
sum += count % 2
}

return sum < 2
}
}
```

<!-- tabs:end -->

### Solution 2: Another Implementation of Hash Table
Expand Down
15 changes: 15 additions & 0 deletions lcci/01.04.Palindrome Permutation/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
func canPermutePalindrome(_ s: String) -> Bool {
var cnt = [Character: Int]()
for char in s {
cnt[char, default: 0] += 1
}

var sum = 0
for count in cnt.values {
sum += count % 2
}

return sum < 2
}
}
42 changes: 42 additions & 0 deletions lcci/01.05.One Away/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,48 @@ impl Solution {
}
```

```swift
class Solution {
func oneEditAway(_ first: String, _ second: String) -> Bool {
let m = first.count, n = second.count
if m < n {
return oneEditAway(second, first)
}
if m - n > 1 {
return false
}

var cnt = 0
var firstIndex = first.startIndex
var secondIndex = second.startIndex

if m == n {
while secondIndex != second.endIndex {
if first[firstIndex] != second[secondIndex] {
cnt += 1
if cnt > 1 {
return false
}
}
firstIndex = first.index(after: firstIndex)
secondIndex = second.index(after: secondIndex)
}
return true
} else {
while firstIndex != first.endIndex {
if secondIndex == second.endIndex || (secondIndex != second.endIndex && first[firstIndex] != second[secondIndex]) {
cnt += 1
} else {
secondIndex = second.index(after: secondIndex)
}
firstIndex = first.index(after: firstIndex)
}
}
return cnt < 2
}
}
```

<!-- tabs:end -->

<!-- end -->
42 changes: 42 additions & 0 deletions lcci/01.05.One Away/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,48 @@ impl Solution {
}
```

```swift
class Solution {
func oneEditAway(_ first: String, _ second: String) -> Bool {
let m = first.count, n = second.count
if m < n {
return oneEditAway(second, first)
}
if m - n > 1 {
return false
}

var cnt = 0
var firstIndex = first.startIndex
var secondIndex = second.startIndex

if m == n {
while secondIndex != second.endIndex {
if first[firstIndex] != second[secondIndex] {
cnt += 1
if cnt > 1 {
return false
}
}
firstIndex = first.index(after: firstIndex)
secondIndex = second.index(after: secondIndex)
}
return true
} else {
while firstIndex != first.endIndex {
if secondIndex == second.endIndex || (secondIndex != second.endIndex && first[firstIndex] != second[secondIndex]) {
cnt += 1
} else {
secondIndex = second.index(after: secondIndex)
}
firstIndex = first.index(after: firstIndex)
}
}
return cnt < 2
}
}
```

<!-- tabs:end -->

<!-- end -->
39 changes: 39 additions & 0 deletions lcci/01.05.One Away/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution {
func oneEditAway(_ first: String, _ second: String) -> Bool {
let m = first.count, n = second.count
if m < n {
return oneEditAway(second, first)
}
if m - n > 1 {
return false
}

var cnt = 0
var firstIndex = first.startIndex
var secondIndex = second.startIndex

if m == n {
while secondIndex != second.endIndex {
if first[firstIndex] != second[secondIndex] {
cnt += 1
if cnt > 1 {
return false
}
}
firstIndex = first.index(after: firstIndex)
secondIndex = second.index(after: secondIndex)
}
return true
} else {
while firstIndex != first.endIndex {
if secondIndex == second.endIndex || (secondIndex != second.endIndex && first[firstIndex] != second[secondIndex]) {
cnt += 1
} else {
secondIndex = second.index(after: secondIndex)
}
firstIndex = first.index(after: firstIndex)
}
}
return cnt < 2
}
}

0 comments on commit 696d844

Please sign in to comment.