From 909c7aa72719a4902195a81cada4fc124655a3e1 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Wed, 1 May 2024 08:19:22 +0100 Subject: [PATCH 1/2] Swift Implementation for LCCI 08.14 --- lcci/08.14.Boolean Evaluation/README.md | 51 ++++++++++++++++++++ lcci/08.14.Boolean Evaluation/README_EN.md | 51 ++++++++++++++++++++ lcci/08.14.Boolean Evaluation/Solution.swift | 48 ++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 lcci/08.14.Boolean Evaluation/Solution.swift diff --git a/lcci/08.14.Boolean Evaluation/README.md b/lcci/08.14.Boolean Evaluation/README.md index 68e3711020cf1..50af3d3442e8c 100644 --- a/lcci/08.14.Boolean Evaluation/README.md +++ b/lcci/08.14.Boolean Evaluation/README.md @@ -190,6 +190,57 @@ func countEval(s string, result int) int { } ``` +```swift +class Solution { + private var memo = [String: [Int]]() + + func countEval(_ s: String, _ result: Int) -> Int { + memo = [:] + let ans = dfs(s) + return result == 0 || result == 1 ? ans[result] : 0 + } + + private func dfs(_ s: String) -> [Int] { + if let res = memo[s] { + return res + } + + var res = [0, 0] + if s.count == 1 { + res[Int(String(s))!] = 1 + return res + } + + for k in 0.. diff --git a/lcci/08.14.Boolean Evaluation/README_EN.md b/lcci/08.14.Boolean Evaluation/README_EN.md index 3473013ea8f85..05cd98b93c0d7 100644 --- a/lcci/08.14.Boolean Evaluation/README_EN.md +++ b/lcci/08.14.Boolean Evaluation/README_EN.md @@ -201,6 +201,57 @@ func countEval(s string, result int) int { } ``` +```swift +class Solution { + private var memo = [String: [Int]]() + + func countEval(_ s: String, _ result: Int) -> Int { + memo = [:] + let ans = dfs(s) + return result == 0 || result == 1 ? ans[result] : 0 + } + + private func dfs(_ s: String) -> [Int] { + if let res = memo[s] { + return res + } + + var res = [0, 0] + if s.count == 1 { + res[Int(String(s))!] = 1 + return res + } + + for k in 0.. diff --git a/lcci/08.14.Boolean Evaluation/Solution.swift b/lcci/08.14.Boolean Evaluation/Solution.swift new file mode 100644 index 0000000000000..c03e43e18ec58 --- /dev/null +++ b/lcci/08.14.Boolean Evaluation/Solution.swift @@ -0,0 +1,48 @@ +class Solution { + private var memo = [String: [Int]]() + + func countEval(_ s: String, _ result: Int) -> Int { + memo = [:] + let ans = dfs(s) + return result == 0 || result == 1 ? ans[result] : 0 + } + + private func dfs(_ s: String) -> [Int] { + if let res = memo[s] { + return res + } + + var res = [0, 0] + if s.count == 1 { + res[Int(String(s))!] = 1 + return res + } + + for k in 0.. Date: Wed, 1 May 2024 15:00:46 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- lcci/08.14.Boolean Evaluation/README.md | 4 ++-- lcci/08.14.Boolean Evaluation/README_EN.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lcci/08.14.Boolean Evaluation/README.md b/lcci/08.14.Boolean Evaluation/README.md index 50af3d3442e8c..a2a0fc731c527 100644 --- a/lcci/08.14.Boolean Evaluation/README.md +++ b/lcci/08.14.Boolean Evaluation/README.md @@ -204,7 +204,7 @@ class Solution { if let res = memo[s] { return res } - + var res = [0, 0] if s.count == 1 { res[Int(String(s))!] = 1 @@ -218,7 +218,7 @@ class Solution { if op == "&" || op == "|" || op == "^" { let left = dfs(String(s[s.startIndex..