From ba050868f53965afda22e6a6599e52231a1528c9 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Fri, 12 Apr 2024 11:19:59 +0100 Subject: [PATCH 1/2] Swift implementation for lcci 01.04 and 01.05 Signed-off-by: Lanre Adedara --- lcci/01.04.Palindrome Permutation/README.md | 18 ++++++++ .../01.04.Palindrome Permutation/README_EN.md | 18 ++++++++ .../Solution.swift | 15 +++++++ lcci/01.05.One Away/README.md | 42 +++++++++++++++++++ lcci/01.05.One Away/README_EN.md | 42 +++++++++++++++++++ lcci/01.05.One Away/Solution.swift | 39 +++++++++++++++++ 6 files changed, 174 insertions(+) create mode 100644 lcci/01.04.Palindrome Permutation/Solution.swift create mode 100644 lcci/01.05.One Away/Solution.swift diff --git a/lcci/01.04.Palindrome Permutation/README.md b/lcci/01.04.Palindrome Permutation/README.md index 8f209949ca354..d444611ff0fc8 100644 --- a/lcci/01.04.Palindrome Permutation/README.md +++ b/lcci/01.04.Palindrome Permutation/README.md @@ -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 + } +} +``` + ### 方法二:哈希表的另一种实现 diff --git a/lcci/01.04.Palindrome Permutation/README_EN.md b/lcci/01.04.Palindrome Permutation/README_EN.md index 155a8c2885f98..cbe02a500eb75 100644 --- a/lcci/01.04.Palindrome Permutation/README_EN.md +++ b/lcci/01.04.Palindrome Permutation/README_EN.md @@ -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 + } +} +``` + ### Solution 2: Another Implementation of Hash Table diff --git a/lcci/01.04.Palindrome Permutation/Solution.swift b/lcci/01.04.Palindrome Permutation/Solution.swift new file mode 100644 index 0000000000000..679aaec63380f --- /dev/null +++ b/lcci/01.04.Palindrome Permutation/Solution.swift @@ -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 + } +} diff --git a/lcci/01.05.One Away/README.md b/lcci/01.05.One Away/README.md index 7f9317db7dd8c..5b6571d0f544c 100644 --- a/lcci/01.05.One Away/README.md +++ b/lcci/01.05.One Away/README.md @@ -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 + } +} +``` + diff --git a/lcci/01.05.One Away/README_EN.md b/lcci/01.05.One Away/README_EN.md index 9456059ccdaff..16bbc786364f4 100644 --- a/lcci/01.05.One Away/README_EN.md +++ b/lcci/01.05.One Away/README_EN.md @@ -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 + } +} +``` + diff --git a/lcci/01.05.One Away/Solution.swift b/lcci/01.05.One Away/Solution.swift new file mode 100644 index 0000000000000..03854b8e46568 --- /dev/null +++ b/lcci/01.05.One Away/Solution.swift @@ -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 + } +} From 0d78cac31c2004b4e1ab0dda0efcc016d26e4267 Mon Sep 17 00:00:00 2001 From: klever34 Date: Fri, 12 Apr 2024 10:35:52 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- lcci/01.04.Palindrome Permutation/README.md | 4 ++-- lcci/01.04.Palindrome Permutation/README_EN.md | 4 ++-- lcci/01.05.One Away/README.md | 4 ++-- lcci/01.05.One Away/README_EN.md | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lcci/01.04.Palindrome Permutation/README.md b/lcci/01.04.Palindrome Permutation/README.md index d444611ff0fc8..55825804220e5 100644 --- a/lcci/01.04.Palindrome Permutation/README.md +++ b/lcci/01.04.Palindrome Permutation/README.md @@ -127,12 +127,12 @@ class Solution { for char in s { cnt[char, default: 0] += 1 } - + var sum = 0 for count in cnt.values { sum += count % 2 } - + return sum < 2 } } diff --git a/lcci/01.04.Palindrome Permutation/README_EN.md b/lcci/01.04.Palindrome Permutation/README_EN.md index cbe02a500eb75..20ac9f21ba985 100644 --- a/lcci/01.04.Palindrome Permutation/README_EN.md +++ b/lcci/01.04.Palindrome Permutation/README_EN.md @@ -124,12 +124,12 @@ class Solution { for char in s { cnt[char, default: 0] += 1 } - + var sum = 0 for count in cnt.values { sum += count % 2 } - + return sum < 2 } } diff --git a/lcci/01.05.One Away/README.md b/lcci/01.05.One Away/README.md index 5b6571d0f544c..c05e9298507b2 100644 --- a/lcci/01.05.One Away/README.md +++ b/lcci/01.05.One Away/README.md @@ -233,11 +233,11 @@ class Solution { 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] { diff --git a/lcci/01.05.One Away/README_EN.md b/lcci/01.05.One Away/README_EN.md index 16bbc786364f4..fe45ae237b16e 100644 --- a/lcci/01.05.One Away/README_EN.md +++ b/lcci/01.05.One Away/README_EN.md @@ -241,11 +241,11 @@ class Solution { 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] {