From 696d844ecde427b3127da658a230b6827bcca0bc Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Sat, 13 Apr 2024 02:30:38 +0100 Subject: [PATCH] feat: add solutions to lcci problems: No.01.04,01.05 (#2577) --- 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..55825804220e5 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..20ac9f21ba985 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..c05e9298507b2 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..fe45ae237b16e 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 + } +}