Skip to content

feat: add solutions to lcci problems: No.01.04,01.05 #2577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}