From 39d459c2df2a3690f0a0bec42a5ba1355a6488a0 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Wed, 8 May 2024 07:56:13 +0100 Subject: [PATCH 1/2] Swift Implementation for LCCI 16.18 --- lcci/16.18.Pattern Matching/README.md | 66 ++++++++++++++++++++++ lcci/16.18.Pattern Matching/README_EN.md | 66 ++++++++++++++++++++++ lcci/16.18.Pattern Matching/Solution.swift | 63 +++++++++++++++++++++ lcci/16.19.Pond Sizes/Solution.java | 2 +- 4 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 lcci/16.18.Pattern Matching/Solution.swift diff --git a/lcci/16.18.Pattern Matching/README.md b/lcci/16.18.Pattern Matching/README.md index 05fcc2096fadc..0a55bd9eeb3bb 100644 --- a/lcci/16.18.Pattern Matching/README.md +++ b/lcci/16.18.Pattern Matching/README.md @@ -292,6 +292,72 @@ function patternMatching(pattern: string, value: string): boolean { } ``` +```swift +class Solution { + private var pattern: String = "" + private var value: String = "" + + func patternMatching(_ pattern: String, _ value: String) -> Bool { + self.pattern = pattern + self.value = value + var cnt = [Int](repeating: 0, count: 2) + for c in pattern { + cnt[Int(c.asciiValue! - Character("a").asciiValue!)] += 1 + } + let n = value.count + if cnt[0] == 0 { + return n % cnt[1] == 0 && String(repeating: String(value.prefix(n / cnt[1])), count: cnt[1]) == value + } + if cnt[1] == 0 { + return n % cnt[0] == 0 && String(repeating: String(value.prefix(n / cnt[0])), count: cnt[0]) == value + } + for la in 0...n { + if la * cnt[0] > n { + break + } + if (n - la * cnt[0]) % cnt[1] == 0 { + let lb = (n - la * cnt[0]) / cnt[1] + if check(la, lb) { + return true + } + } + } + return false + } + + private func check(_ la: Int, _ lb: Int) -> Bool { + var a: String? = nil + var b: String? = nil + var index = value.startIndex + + for c in pattern { + if c == "a" { + let end = value.index(index, offsetBy: la) + if let knownA = a { + if knownA != value[index.. diff --git a/lcci/16.18.Pattern Matching/README_EN.md b/lcci/16.18.Pattern Matching/README_EN.md index c3fb745452eb0..c3dd1802fa956 100644 --- a/lcci/16.18.Pattern Matching/README_EN.md +++ b/lcci/16.18.Pattern Matching/README_EN.md @@ -307,6 +307,72 @@ function patternMatching(pattern: string, value: string): boolean { } ``` +```swift +class Solution { + private var pattern: String = "" + private var value: String = "" + + func patternMatching(_ pattern: String, _ value: String) -> Bool { + self.pattern = pattern + self.value = value + var cnt = [Int](repeating: 0, count: 2) + for c in pattern { + cnt[Int(c.asciiValue! - Character("a").asciiValue!)] += 1 + } + let n = value.count + if cnt[0] == 0 { + return n % cnt[1] == 0 && String(repeating: String(value.prefix(n / cnt[1])), count: cnt[1]) == value + } + if cnt[1] == 0 { + return n % cnt[0] == 0 && String(repeating: String(value.prefix(n / cnt[0])), count: cnt[0]) == value + } + for la in 0...n { + if la * cnt[0] > n { + break + } + if (n - la * cnt[0]) % cnt[1] == 0 { + let lb = (n - la * cnt[0]) / cnt[1] + if check(la, lb) { + return true + } + } + } + return false + } + + private func check(_ la: Int, _ lb: Int) -> Bool { + var a: String? = nil + var b: String? = nil + var index = value.startIndex + + for c in pattern { + if c == "a" { + let end = value.index(index, offsetBy: la) + if let knownA = a { + if knownA != value[index.. diff --git a/lcci/16.18.Pattern Matching/Solution.swift b/lcci/16.18.Pattern Matching/Solution.swift new file mode 100644 index 0000000000000..ed9d4f729c9b4 --- /dev/null +++ b/lcci/16.18.Pattern Matching/Solution.swift @@ -0,0 +1,63 @@ +class Solution { + private var pattern: String = "" + private var value: String = "" + + func patternMatching(_ pattern: String, _ value: String) -> Bool { + self.pattern = pattern + self.value = value + var cnt = [Int](repeating: 0, count: 2) + for c in pattern { + cnt[Int(c.asciiValue! - Character("a").asciiValue!)] += 1 + } + let n = value.count + if cnt[0] == 0 { + return n % cnt[1] == 0 && String(repeating: String(value.prefix(n / cnt[1])), count: cnt[1]) == value + } + if cnt[1] == 0 { + return n % cnt[0] == 0 && String(repeating: String(value.prefix(n / cnt[0])), count: cnt[0]) == value + } + for la in 0...n { + if la * cnt[0] > n { + break + } + if (n - la * cnt[0]) % cnt[1] == 0 { + let lb = (n - la * cnt[0]) / cnt[1] + if check(la, lb) { + return true + } + } + } + return false + } + + private func check(_ la: Int, _ lb: Int) -> Bool { + var a: String? = nil + var b: String? = nil + var index = value.startIndex + + for c in pattern { + if c == "a" { + let end = value.index(index, offsetBy: la) + if let knownA = a { + if knownA != value[index.. Date: Wed, 8 May 2024 08:16:44 +0100 Subject: [PATCH 2/2] removed spaces --- lcci/16.19.Pond Sizes/Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lcci/16.19.Pond Sizes/Solution.java b/lcci/16.19.Pond Sizes/Solution.java index aec557ac282b6..6bb6b2c544752 100644 --- a/lcci/16.19.Pond Sizes/Solution.java +++ b/lcci/16.19.Pond Sizes/Solution.java @@ -30,4 +30,4 @@ private int dfs(int i, int j) { } return res; } -} \ No newline at end of file +} \ No newline at end of file