diff --git a/lcci/08.14.Boolean Evaluation/README.md b/lcci/08.14.Boolean Evaluation/README.md index 68e3711020cf1..a2a0fc731c527 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..eaed9d1b3b137 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..