Skip to content

Commit b0cfeee

Browse files
committed
Refactor solutions to Valid Sudoku and Serialize and Deserialize Binary Tree
1 parent 378d7b1 commit b0cfeee

File tree

2 files changed

+101
-41
lines changed

2 files changed

+101
-41
lines changed

Array/ValidSudoku.swift

+34-38
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66
*/
77

88
class ValidSudoku {
9-
let size = 9
10-
11-
func isValidSudoku(board: [[Character]]) -> Bool {
12-
return _isRowValid(board) && _isColValid(board) && _isSquareValid(board)
9+
func isValidSudoku(_ board: [[Character]]) -> Bool {
10+
return areRowsValid(board) && areColsValid(board) && areSubsquaresValid(board)
1311
}
1412

15-
private func _isRowValid(board: [[Character]]) -> Bool {
16-
var visited = Array(count: size, repeatedValue: false)
13+
private func areRowsValid(_ board: [[Character]]) -> Bool {
14+
var existingDigits = Set<Character>()
1715

18-
for i in 0..<size {
19-
visited = Array(count: size, repeatedValue: false)
20-
for j in 0..<size {
21-
if !_isValidChar(board[i][j], &visited) {
16+
for i in 0..<board.count {
17+
existingDigits.removeAll()
18+
19+
for j in 0..<board[0].count {
20+
if !isDigitValid(board[i][j], &existingDigits) {
2221
return false
2322
}
2423
}
@@ -27,13 +26,14 @@ class ValidSudoku {
2726
return true
2827
}
2928

30-
private func _isColValid(board: [[Character]]) -> Bool {
31-
var visited = Array(count: size, repeatedValue: false)
32-
33-
for i in 0..<size {
34-
visited = Array(count: size, repeatedValue: false)
35-
for j in 0..<size {
36-
if !_isValidChar(board[j][i], &visited) {
29+
private func areColsValid(_ board: [[Character]]) -> Bool {
30+
var existingDigits = Set<Character>()
31+
32+
for i in 0..<board[0].count {
33+
existingDigits.removeAll()
34+
35+
for j in 0..<board.count {
36+
if !isDigitValid(board[j][i], &existingDigits) {
3737
return false
3838
}
3939
}
@@ -42,15 +42,16 @@ class ValidSudoku {
4242
return true
4343
}
4444

45-
private func _isSquareValid(board: [[Character]]) -> Bool {
46-
var visited = Array(count: size, repeatedValue: false)
45+
private func areSubsquaresValid(_ board: [[Character]]) -> Bool {
46+
var existingDigits = Set<Character>()
4747

48-
for i in 0.stride(to: size, by: 3) {
49-
for j in 0.stride(to: size, by: 3) {
50-
visited = Array(count: size, repeatedValue: false)
48+
for i in stride(from: 0, to: board.count, by: 3) {
49+
for j in stride(from: 0, to: board[0].count, by: 3) {
50+
existingDigits.removeAll()
51+
5152
for m in i..<i + 3 {
5253
for n in j..<j + 3 {
53-
if !_isValidChar(board[m][n], &visited) {
54+
if !isDigitValid(board[m][n], &existingDigits) {
5455
return false
5556
}
5657
}
@@ -61,21 +62,16 @@ class ValidSudoku {
6162
return true
6263
}
6364

64-
private func _isValidChar(char: Character, inout _ visited: [Bool]) -> Bool {
65-
let current = String(char)
66-
67-
if current != "." {
68-
if let num = Int(current){
69-
if num < 1 || num > 9 || visited[num - 1] {
70-
return false
71-
} else {
72-
visited[num - 1] = true
73-
}
74-
} else {
75-
return false
76-
}
77-
}
65+
private func isDigitValid(_ digit: Character, _ set: inout Set<Character>) -> Bool {
66+
if digit == "." {
67+
return true
68+
}
7869

79-
return true
70+
if set.contains(digit) {
71+
return false
72+
} else {
73+
set.insert(digit)
74+
return true
75+
}
8076
}
8177
}

Tree/SerializeDeserializeBinaryTree.swift

+67-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Question Link: https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
3-
* Primary idea: Preorder
3+
* Primary idea: Preorder or level order
44
* Time Complexity: O(n), Space Complexity: O(n)
55
*
66
* Definition for a binary tree node.
@@ -16,7 +16,8 @@
1616
* }
1717
*/
1818

19-
class SerializeDeserializeBinaryTree {
19+
# Solution 1 - Preorder
20+
class SerializeDeserializeBinaryTree {
2021
func serialize(_ root: Node?) -> String {
2122
guard let root = root else {
2223
return "#"
@@ -36,4 +37,67 @@
3637

3738
return root
3839
}
39-
}
40+
}
41+
42+
# Solution 2 - Level order, BFS
43+
class SerializeDeserializeBinaryTree {
44+
func serialize(_ root: Node?) -> String {
45+
guard let root = root else {
46+
return ""
47+
}
48+
49+
var res = "", nodes: [Node?] = [root]
50+
51+
while !nodes.isEmpty {
52+
let currentLevelSize = nodes.count
53+
54+
for _ in 0..<currentLevelSize {
55+
let node = nodes.removeFirst()
56+
57+
if let node = node {
58+
res.append(String(node.val))
59+
60+
nodes.append(node.left)
61+
nodes.append(node.right)
62+
} else {
63+
res.append("#")
64+
}
65+
}
66+
}
67+
68+
return res
69+
}
70+
71+
func deserialize(_ vals: String) -> Node? {
72+
guard let firstVal = vals.first, let rootVal = Int(String(firstVal)) else {
73+
return nil
74+
}
75+
76+
let root = Node(rootVal), vals = Array(vals)
77+
var nodes: [Node?] = [root], i = 1
78+
79+
while !nodes.isEmpty {
80+
guard let node = nodes.removeFirst() else {
81+
continue
82+
}
83+
84+
var left: Node?
85+
if let leftVal = Int(String(vals[i])) {
86+
left = Node(leftVal)
87+
}
88+
node.left = left
89+
nodes.append(left)
90+
i += 1
91+
92+
var right: Node?
93+
if let rightVal = Int(String(vals[i])) {
94+
right = Node(rightVal)
95+
}
96+
node.right = right
97+
nodes.append(right)
98+
i += 1
99+
}
100+
101+
return root
102+
}
103+
}

0 commit comments

Comments
 (0)