Skip to content

feat: add swift implementation to lcci problem: No.08.14 #2706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions lcci/08.14.Boolean Evaluation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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..<s.count {
let index = s.index(s.startIndex, offsetBy: k)
let op = String(s[index])

if op == "&" || op == "|" || op == "^" {
let left = dfs(String(s[s.startIndex..<index]))
let right = dfs(String(s[s.index(after: index)...]))

for i in 0...1 {
for j in 0...1 {
var v = 0
if op == "&" {
v = i & j
} else if op == "|" {
v = i | j
} else if op == "^" {
v = i ^ j
}
res[v] += left[i] * right[j]
}
}
}
}

memo[s] = res
return res
}
}
```

<!-- tabs:end -->

<!-- end -->
51 changes: 51 additions & 0 deletions lcci/08.14.Boolean Evaluation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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..<s.count {
let index = s.index(s.startIndex, offsetBy: k)
let op = String(s[index])

if op == "&" || op == "|" || op == "^" {
let left = dfs(String(s[s.startIndex..<index]))
let right = dfs(String(s[s.index(after: index)...]))

for i in 0...1 {
for j in 0...1 {
var v = 0
if op == "&" {
v = i & j
} else if op == "|" {
v = i | j
} else if op == "^" {
v = i ^ j
}
res[v] += left[i] * right[j]
}
}
}
}

memo[s] = res
return res
}
}
```

<!-- tabs:end -->

<!-- end -->
48 changes: 48 additions & 0 deletions lcci/08.14.Boolean Evaluation/Solution.swift
Original file line number Diff line number Diff line change
@@ -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..<s.count {
let index = s.index(s.startIndex, offsetBy: k)
let op = String(s[index])

if op == "&" || op == "|" || op == "^" {
let left = dfs(String(s[s.startIndex..<index]))
let right = dfs(String(s[s.index(after: index)...]))

for i in 0...1 {
for j in 0...1 {
var v = 0
if op == "&" {
v = i & j
} else if op == "|" {
v = i | j
} else if op == "^" {
v = i ^ j
}
res[v] += left[i] * right[j]
}
}
}
}

memo[s] = res
return res
}
}